String to decimal conversion: dot separation instead of comma

前端 未结 7 1223
野的像风
野的像风 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);

    0 讨论(0)
  • 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"));
    
    0 讨论(0)
  • 2020-12-05 02:27

    I ended up using this solution.

    decimal weeklyWage;
    decimal.TryParse(items[2],NumberStyles.Any, new NumberFormatInfo() { NumberDecimalSeparator = "."}, out weeklyWage);
    
    0 讨论(0)
  • 2020-12-05 02:30

    Instead of replace we can force culture like

    var x = decimal.Parse("18,285", new NumberFormatInfo() { NumberDecimalSeparator = "," });
    

    it will give output 18.285

    0 讨论(0)
  • 2020-12-05 02:30

    Thanks for all reply.

    Because I have to write a decimal number in a xml file I have find out the problem. In this discussion I have learned that xml file standard use dot for decimal value and this is culture independent. So my solution is write dot decimal number in a xml file and convert the readed string from the same xml file mystring.Replace(".", ","); Thanks Agat for suggestion to research the problem in xml context and Ε Г И І И О because I didn't know visual studio doesn't respect the culture settings I have in my code.

    0 讨论(0)
  • 2020-12-05 02:33
        usCulture = new CultureInfo("vi-VN");
    Thread.CurrentThread.CurrentCulture = usCulture;
    Thread.CurrentThread.CurrentUICulture = usCulture;
    usCulture = Thread.CurrentThread.CurrentCulture;
    dbNumberFormat = usCulture.NumberFormat;
    number = decimal.Parse("1.332,23", dbNumberFormat); //123.456.789,00
    
    usCulture = new CultureInfo("en-GB");
    Thread.CurrentThread.CurrentCulture = usCulture;
    Thread.CurrentThread.CurrentUICulture = usCulture;
    usCulture = Thread.CurrentThread.CurrentCulture;
    dbNumberFormat = usCulture.NumberFormat;
    number = decimal.Parse("1,332.23", dbNumberFormat); //123.456.789,00
    
    /*Decision*/
    var usCulture = Thread.CurrentThread.CurrentCulture;
    var dbNumberFormat = usCulture.NumberFormat;
    decimal number;
    decimal.TryParse("1,332.23", dbNumberFormat, out number); //123.456.789,00
    
    0 讨论(0)
提交回复
热议问题