Best way to convert string to decimal separator “.” and “,” insensitive way?

前端 未结 7 1165
北海茫月
北海茫月 2020-12-15 06:05

Application deals with strings that represent decimals that come from different cultures. For example \"1.1 and \"1,1\" is the same value.

I played with Deci

7条回答
  •  攒了一身酷
    2020-12-15 06:45

    You just need to have the correct culture set, when calling Parse, like so:

    string s = "11,20";
    
    decimal c1 = decimal.Parse(s, new CultureInfo("fr-FR"));
    decimal c2 = decimal.Parse(s, new CultureInfo("en-AU"));
    
    Console.WriteLine(c1);
    Console.WriteLine(c2);
    

提交回复
热议问题