String to decimal conversion: dot separation instead of comma

前端 未结 7 1247
野的像风
野的像风 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条回答
  •  Happy的楠姐
    2020-12-05 02:27

    All this is about cultures. If you have any other culture than "US English" (and also as good manners of development), you should use something like this:

    var d = Convert.ToDecimal("1.2345", new CultureInfo("en-US"));
    // (or 1,2345 with your local culture, for instance)
    

    (obviously, you should replace the "en-US" with the culture of your number local culture)

    the same way, if you want to do ToString()

    d.ToString(new CultureInfo("en-US"));
    

提交回复
热议问题