开发者

Culture problem reading formatted decimal values

开发者 https://www.devze.com 2022-12-11 11:57 出处:网络
I have an console application (that also is run as a service when deployed). I\'m reading values that originates from a text file. The problem is that I can\'t get the decimal values to work properly.

I have an console application (that also is run as a service when deployed). I'm reading values that originates from a text file. The problem is that I can't get the decimal values to work properly.

They are formatted in a "swedish way", like this 3,4 which in "english" formatting is 3.4.

When I use:

Decimal.TryParse(value, NumberStyles.Number, new CultureInfo("sv"), out result);

I get a

Culture 'sv' is a neutral culture. It cannot be used in formatting and parsing and therefore cannot be set as the thread's current cu开发者_开发问答lture.

exception.

If I try:

Decimal.TryParse(value, out result);

The value gets trucated (3,4 becomes 3).

Is there a way to read these formatted values (which can be integers also) in a robust and safe way, regardless of the settings in the operating system.


You need to use "sv-SE"

"sv" defaults to "sv-EN" you need to specify a country then a language in this case.

static void Main(string[] args)
{
  decimal result;
  Decimal.TryParse("2,3", NumberStyles.Number, new CultureInfo("sv-SE"), 
                   out result); 
  Console.WriteLine(result);
  Console.ReadLine();
}

Result 2.3

0

精彩评论

暂无评论...
验证码 换一张
取 消