How to convert "12,4" to decimal en-Us culture
Asked Answered
A

7

15

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

Allpowerful answered 1/12, 2011 at 13:0 Comment(3)
Can you be explicit? is that meant to be 133 decimal-point 3, or 1333...?Prasad
check this one as well: #832227Sadiron
"133,3" in Norwagian culture comma is used as decimal separator.Allpowerful
N
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.

Neuralgia answered 1/12, 2011 at 13:3 Comment(1)
+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.Delivery
H
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();
      }                     
   }
}
Hoopes answered 1/12, 2011 at 13:6 Comment(1)
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 :/Cloudburst
B
9

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.

Brainstorm answered 1/12, 2011 at 13:4 Comment(0)
A
6
Convert.ToDouble(textBox2.Text, new CultureInfo("uk-UA")).ToString(new CultureInfo("en-US"));
Avar answered 13/4, 2012 at 5:47 Comment(1)
Welcome to stackoverflow! It's always better to provide a short description for a sample code to improve the post accuracy :)Newsman
P
2

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

Pyridoxine answered 27/2, 2016 at 14:59 Comment(0)
D
1

Hope it's helpful

double _number = 12536,8;

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

return _number.ToString("N", us);
Delgado answered 23/6, 2020 at 8:33 Comment(0)
A
-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);
    }
Allpowerful answered 2/12, 2011 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.