0

I have two string : 1.5 and 1,5

I want to convert this to decimal but I want to detect automatically the culture : with point us culture and with comma fr culture

What is the best way to do this ?

Liam
  • 27,717
  • 28
  • 128
  • 190
  • 3
    You can't do this with 100% certainty, because the comma could mean a German or a Russian culture, and the dot can be an US or an UK culture. – dymanoid Nov 17 '16 at 08:50
  • 1
    Even worse, what does `1,543` mean? It could be `1.543` or `1543` depending on culture. – Matthew Watson Nov 17 '16 at 08:52
  • If the rules you want to follow **are that simple** then do what you said, look for a point, if you find one then you have us culture, if you find a comma then you have the fr culture. What is the question here? The best way to do **exactly** what you asked for is 1-2 if-statements. – Lasse V. Karlsen Nov 17 '16 at 08:52
  • 1
    If, on the other hand, those two cultures are merely examples of what you want and you want the full range of cultures then no, that can't be done. Simple as that. – Lasse V. Karlsen Nov 17 '16 at 08:53
  • How to get simply the NumberDecimalSeparator of my string ? I want to get a char. Thanks –  Nov 17 '16 at 08:59

2 Answers2

2

If your numbers can't contain thousand group separators you could use this:

var usCulture = new CultureInfo("en-US");
var frCulture = new CultureInfo("fr-FR");
NumberStyles ns = NumberStyles.Any & ~NumberStyles.AllowThousands;  // without thousands

string[] strings = {"1.5", "1,5"};
foreach (string s in strings)
{
    decimal d;
    bool isUsCulture = decimal.TryParse(s, ns, usCulture, out d);
    bool isFrCulture = decimal.TryParse(s, ns, frCulture, out d);
}

Note that you can now detect the culture but only if it contains the decimal separator. A number like "2" could be us or fr.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • If I may attach to this question... If I had an application in multiple countries with different number styles, what would be the best way to add an value (number) to my database, and make the number 'universal'? – sharp Nov 17 '16 at 09:20
  • @FiN: there is no "universal" number, but you could specify the culture. However, your question doesnt make sense because a number should be stored as number and not as string, f.e. as `DECIMAL(18,4)`. – Tim Schmelter Nov 17 '16 at 09:31
  • That's why I'm asking, if i get the value of field where it says `1,24` what will happen... Will it save it to the database as `1.24` or... – sharp Nov 17 '16 at 09:33
  • That depends on your code which you havent shown. `decimal.TryParse(s, ns, frCulture, out d)` will parse it to a decimal. Then just use a sql-parameter with `SqlDbType.Decimal`. Of course it must be a decimal in the database too. – Tim Schmelter Nov 17 '16 at 09:41
  • This is just a question, nothing with hands on... Thanks for the advice :) – sharp Nov 17 '16 at 09:45
0

You can try this: you insert this code in line first on function program.
you use to the separators "." eg:decimal.Parse("0.2")

var usCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = usCulture;
Thread.CurrentThread.CurrentUICulture = usCulture ;
sharp
  • 1,191
  • 14
  • 39