How to convert “12,4” to decimal en-Us culture

前端 未结 7 2163
梦如初夏
梦如初夏 2020-12-16 02:24

I have a decimal value (\"133,3\") stored in string column in the database, in norway culture.

after that user changed the regional setting to english-Us. when I con

相关标签:
7条回答
  • 2020-12-16 03:03

    used below code to fix my issue. I just hard coded the previous currency decimal part. may not be generic. but solved my problem.

    public static decimal? ToDecimal1(this string source)
        {
            CultureInfo usCulture = new CultureInfo("en-US");
    
            if (string.IsNullOrEmpty(source.Trim1()))
                return null;
            else
                return Convert.ToDecimal(source.Replace(",", ".").Trim(), usCulture);
        }
    
    0 讨论(0)
  • 2020-12-16 03:09

    Do you referred to Convert.ToDecimal(), it says like

    using System;
    using System.Globalization;
    
    public class Example
    {
       public static void Main()
       {
          string[] values = { "123456789", "12345.6789", "12 345,6789",
                              "123,456.789", "123 456,789", "123,456,789.0123",
                              "123 456 789,0123" };
          CultureInfo[] cultures = { new CultureInfo("en-US"),
                                     new CultureInfo("fr-FR") }; 
    
          foreach (CultureInfo culture in cultures)
          {
             Console.WriteLine("String -> Decimal Conversion Using the {0} Culture",
                               culture.Name);
             foreach (string value in values)
             {
                Console.Write("{0,20}  ->  ", value);
                try {
                   Console.WriteLine(Convert.ToDecimal(value, culture));
                }
                catch (FormatException) {
                   Console.WriteLine("FormatException");
                }
             }
             Console.WriteLine();
          }                     
       }
    }
    
    0 讨论(0)
  • 2020-12-16 03:10

    Regardless of the system culture, if you specify CultureInfo.InvariantCulture you won't be able to parse "133,3" as a decimal to 133.3. The same is true for US English.

    You could just specify a Norwegian culture when parsing the value (using the overload of decimal.TryParse which takes an IFormatProvider), or (preferrably) change the field in the database to reflect the real data type (a decimal number) instead.

    0 讨论(0)
  • 2020-12-16 03:10

    This solves your problem: .ToString(New CultureInfo("en-US"))

    0 讨论(0)
  • 2020-12-16 03:15

    Hope it's helpful

    double _number = 12536,8;
    
    CultureInfo usCulture = new CultureInfo("en-US");
    
    return _number.ToString("N", us);
    
    0 讨论(0)
  • 2020-12-16 03:16

    If you know the culture that was in use when persisting the value, you can use it when parsing it, i.e.:

    Convert.ToDecimal("133,3", System.Globalization.CultureInfo.GetCultureInfo("no"));
    

    Of course, you are probably better off changing how the data is stored in the database, to use a floating point number of some form.

    0 讨论(0)
提交回复
热议问题