C# define string format of double/floats to be US english by default
I have got several 开发者_StackOverflow社区thousands of lines of a web application source code, initially written on a US development system, to maintain. It makes heavy use of SQL statement strings, which are combined on the fly, e.g.
string SQL = "select * from table where double_value = " + Math.Round(double_value, 2);
Don't comment on bad programming style, that doesn't help me in this case :)
The crux: My system uses a German locale, which in turn leads to wrong SQL statements, like this:
"select * from table where double_value = 15,5"
(Note the comma as decimal separator instead of a point).
Question: What is the most "elegant" way to change the locale of the web app in this case) to US or UK in order to prevent being forced to change and inspect every single line of code? .NET 3.5 is not an option (would give me the chance to overwrite ToString()
in an extension class.)
You don't need to use an extension method, just use ToString with an IFormatProvider:
string SQL = "select * from table where double_value = "
+ Math.Round(double_value, 2).ToString(CultureInfo.InvariantCulture);
You need to use a string formatter with a default en-US culture (or some other culture that uses . instead of , as a decimal place).
See: http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx
Right after having formed the question I found the answer.
Here it is (to be added to web.config):
<configuration>
<system.web>
<globalization culture="en-US"/>
</system.web>
</configuration>
Thanks
精彩评论