13

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 convert "133,3" to decimal using CultureInfo.InvariantCulture, getting invalid value or error.

is there any best way to handle this scenario in C# application?

regards, Anand

Anand Kumar
  • 395
  • 2
  • 7
  • 18

7 Answers7

21

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    +1 - Yes. Only ever use culture specific code when passing data to/from the user e.g. by screen , email, print etc. A standard decimal in the database would be far better as this would allow code to more easily cope with changes in culture. – ChrisBD Dec 01 '11 at 13:11
11

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();
      }                     
   }
}
  • I tried it with: "en-US" and "de-DE" ----- 1.234,56: US: FormatException; DE: 1234.56 -> OK ----- 1,234.56: US: 1234.56; DE: FormatException -> OK ----- 234.56: US: 234.56; DE: 23456 -> Not OK ----- 234,56: US: 23456; DE: 234.56 -> Not OK ----- Sorry, without line breaks it's a little bit confusing :/ – Beetee Nov 19 '18 at 16:20
8

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.

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
6
Convert.ToDouble(textBox2.Text, new CultureInfo("uk-UA")).ToString(new CultureInfo("en-US"));
Code Lღver
  • 15,573
  • 16
  • 56
  • 75
Shrikant Prabhu
  • 709
  • 8
  • 13
2

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

Chris
  • 21
  • 2
1

Hope it's helpful

double _number = 12536,8;

CultureInfo usCulture = new CultureInfo("en-US");

return _number.ToString("N", us);
Adam
  • 309
  • 3
  • 8
-1

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);
    }
Anand Kumar
  • 395
  • 2
  • 7
  • 18