String to decimal conversion: dot separation instead of comma

前端 未结 7 1248
野的像风
野的像风 2020-12-05 01:52

I have a string read from a textbox. It contains a comma for decimal separation.

I have NumberFormatInfo.CurrencyDecimalSeparator set to ,

7条回答
  •  暖寄归人
    2020-12-05 02:23

    I had faced the similar issue while using Convert.ToSingle(my_value) If the OS language settings is English 2.5 (example) will be taken as 2.5 If the OS language is German, 2.5 will be treated as 2,5 which is 25 I used the invariantculture IFormat provided and it works. It always treats '.' as '.' instead of ',' irrespective of the system language.

    float var = Convert.ToSingle(my_value, System.Globalization.CultureInfo.InvariantCulture);

提交回复
热议问题