user7128918
user7128918

Reputation:

How can I detect french or us culture from string decimal format?

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 ?

Upvotes: 0

Views: 1245

Answers (2)

Nguyễn Văn Hậu
Nguyễn Văn Hậu

Reputation: 1

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 ;

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460138

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.

Upvotes: 2

Related Questions