문제

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

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top