Question

I have installed C# application under Spanish MS Windows Server.

So this code is working in a wrong way.

decimal? top = 80.0m;
double convertedTop = (double)decimal.Parse(top.ToString(), CultureInfo.InvariantCulture); 

convertedTop is 80000 but it should be 80.0

Was it helpful?

Solution

Don't do that.

Your code is extremely inefficient.

You should change it to

double convertedTop = Convert.ToDouble(top);

If the compile-time type of top is decimal or decimal? (as opposed to object or IConvertible or ValueType), you can use an even-more-efficient compile-time cast:

double convertedTop = (double)top;

To answer the question, top.ToString() is culture-sensitive.
You need to pass CultureInfo.InvariantCulture there too.
Nullable<T> doesn't lift ToString(IFormatProvider), so you'll need to do that on Value and handle null explicitly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top