views:

305

answers:

3

I have got several 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.)

A: 

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

mmr
+1  A: 

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);
Reed Copsey
+1  A: 

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

neil
Please remember that this will affect everything language related in your app, including the UI.
Matti Virkkunen