Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert.ToDouble("4089.90") outputs 40.899,00 why?

Tags:

c#

asp.net

I am developing a software that uses number precision, but I have this problem, it happens that when I take a string to convert to double it outputs me with a different culture.

For example I use

Convert.ToDouble("4089.90"); // it outputs 40.899,00

Is strange cause in my computer it works OK but on the client's PC (with the same culture in regional settings) shows me the latter output. I know I can fix it using

Convert.ToDouble("4089.90", System.Globalization.CultureInfo.InvariantCulture);

But there is a lot of code in the program using "Convert.ToDouble" and I wouldn't like to change all of it, on the other hand I want to understand why this happens.

like image 651
Nelson Miranda Avatar asked Jan 12 '10 20:01

Nelson Miranda


3 Answers

You can set the culture for your thread with:

Thread.CurrentThread.CurrentCulture = 
       System.Globalization.CultureInfo.InvariantCulture;
like image 135
dkantowitz Avatar answered Oct 29 '22 07:10

dkantowitz


You don't say where you are based, but the output is consistent with the current culture being one that has "." as the thousands separator and a decimal comma rather than a decimal point.

However, you state that the culture is the same - which contradicts this. Have you or the client changed (or customised) the "Standards and formats" on the Regional and Language Options? If the setting has been customised it will still read as "English (United Kingdom)" (or where ever) but will produce different results to the default.

like image 2
ChrisF Avatar answered Oct 29 '22 06:10

ChrisF


I know neither c# nor asp.net, but I think the problem is this: You are performing the operation in a culture where the dot . is the thousands separator and not the decimal separator. The very output you quote is the proof: 40.899,00.

What culture/locale are you working in?

like image 1
Pekka Avatar answered Oct 29 '22 05:10

Pekka