Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET: Parsing localized currency

Let's say I have a string, and that string's value is an amount of money, localized. By localized, I mean that if the country may use commas instead of decimal points, for example. (That's just one localization difference I know if.)

How can I parse one of these strings into their decimals numeric equivalents? Will decimal.TryParse() recognize localized formatting? How do I specify the CultureInfo with TryParse()?

like image 223
JamesBrownIsDead Avatar asked Jan 21 '10 07:01

JamesBrownIsDead


1 Answers

Here is an example of decimal.TryParse with a specified CultureInfo (Swedish in this case):

string s = "10,95";
decimal d;
if (decimal.TryParse(s, NumberStyles.Number, CultureInfo.GetCultureInfo("sv-SE"),out d))
{
    Console.WriteLine(d);
} 
like image 185
Fredrik Mörk Avatar answered Oct 03 '22 07:10

Fredrik Mörk