.NET:解析本地化货币 [英] .NET: Parsing localized currency
问题描述
假设我有一个 string
,并且 string
的值是本地化的金额.本地化的意思是,例如,如果该国家/地区可以使用逗号而不是小数点.(这只是我所知道的一个本地化差异.)
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.)
如何将这些 string
之一解析为它们的 decimal
数值等效项? decimal.TryParse()
是否可以识别本地化格式?如何使用 TryParse()
指定 CultureInfo
?
How can I parse one of these string
s into their decimal
s numeric equivalents? Will decimal.TryParse()
recognize localized formatting? How do I specify the CultureInfo
with TryParse()
?
推荐答案
下面是带有指定 CultureInfo
(在这种情况下为瑞典语)的 decimal.TryParse
的示例:
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);
}
这篇关于.NET:解析本地化货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!