문제

In my locale the decimal separator is a ','.

However I would still like to write a C# application which works with numbers that use the '.' as decimal separator.

        string b = "0,5";
        double db = double.Parse(b); // gives 0.5

        string a = "0.5";
        double da = double.Parse(a); // gives 5, however i would like to get 0.5
도움이 되었습니까?

해결책

You need to specify the culture as the second argument to double.Parse, e.g.

double da = double.Parse(a, CultureInfo.InvariantCulture);

Pretty much all of the formatting/parsing methods have overloads taking an IFormatProvider, and the most commonly-specified implementation of IFormatProvider is CultureInfo.

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