combining number and dateformating in one funcrtion
-
hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this.
public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above }
So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan. -
hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this.
public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above }
So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan. -
hy everyone! i was wondering if it is possible to combine number formating and dateformating in one function. formating a number is done e.g. by using {#,##0.0000} and date formating is done by using {0:dd/MM/yyyy}. i was wondering if it is possible to separate the numberformating and the dateformating. well, if there are these two, then you might try to get the difference by checking for the ":". But that's no "clean" way to do this.
public static string ConvertFormats (string input, string formatstring) { if (formatstring.Contains(":") { // date } else { // number } return input; //formated in the if else above }
So what if this function should format stringlength as well? then this won't work anymore. Anyone a better idea? thanks! Stephan.Why take a string as input? Even an object would be better:
if ( input is int ) ((int) input).ToString ( formatstring ) ;
else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;But would calling the appropriate ToString directly help?
someint.ToString ( "#,##0.0000" ) ;
somedate.ToString ( "dd/MM/yyyy" ) ;
(Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)
-
Why take a string as input? Even an object would be better:
if ( input is int ) ((int) input).ToString ( formatstring ) ;
else if ( input is System.DateTime ) ((System.DateTime) input).ToString ( formatstring ) ;But would calling the appropriate ToString directly help?
someint.ToString ( "#,##0.0000" ) ;
somedate.ToString ( "dd/MM/yyyy" ) ;
(Of course, I also have to recommend that you use an ISO 8601 date format like "yyyy-MM-dd".)
i parse from a textfile which contains infos like |value|{#,##0.0000} |date|{0:dd/MM/yyyy} when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring. so the input is always a string. i do also provide the culture info which corresponds to the string format so it is formated correctly.
input.ToString(FormatString, new CultureInfo(LocalString));
-
i parse from a textfile which contains infos like |value|{#,##0.0000} |date|{0:dd/MM/yyyy} when parsing the tokens (a token is e.g. |value|) is replaced by it's value and formated by the formatstring behind. so i parse the string after the token and format the string with this formatstring. so the input is always a string. i do also provide the culture info which corresponds to the string format so it is formated correctly.
input.ToString(FormatString, new CultureInfo(LocalString));
Sounds like a bad design; go slap the person responsible. Why doesn't the system that writes the file perform the format?