使用IValueConverter进行货币转换 [英] Currency Conversion using IValueConverter
问题描述
当我的机器时,我试图在IValueConverter中转换数值(实际上来自字符串变量,即500000.000000000000)货币格式在区域设置对话框中设置为西班牙语(智利),导致奇怪的值,如500,000,000,000,000,000.00,我不知道我的代码中究竟出了什么问题。
我的IValueConverter转换器
public object 转换(对象 值,输入targetType, object 参数,System.Globalization.CultureInfo文化)
{
if ( value == null )
return 字符串跨度> .Empty;
string text = value .ToString();
double dbl = 0 。 0 跨度>;
if ( double .TryParse(text, out dbl))
text = dbl.ToString( N2, CultureInfo.InstalledUICulture);
返回文字;
}
有什么猜测为什么会得到这个奇怪的值?
这样的事情如何:
< TextBox 文字 = {Binding Value,StringFormat = N2} / >
< TextBox 文本 = {Binding Value,StringFormat = {} {0:#,#。00}} / >
当你没有将 IFormatProvider
传递给 TryParse
方法时,它默认使用CultureInfo.CurrentCulture [ ^ ] 。默认情况下,这使用当前的区域设置 - 我怀疑西班牙语(智利)使用。
作为千位分隔符,,
作为小数点分隔符,因此该值被解析为 500000000000000000
。
那么你就是使用 InstalledUICulture [ ^ ]格式化值。这是随操作系统安装的文化,不会反映用户的区域设置。这可能设置为英语(美国),使用,
作为千位分隔符,。
作为小数分隔符。
(上有不同文化选项的详细说明StackOverflow [ ^ ]。)
要获得一致的结果,您需要使用相同的格式提供程序来解析和格式化您的值。有一个 CultureInfo
参数传入转换
方法,所以你应该使用它:
public object 转换(对象 值,输入targetType,对象参数,CultureInfo文化)
{
if ( value == null ) return string .Empty;
string text = value .ToString();
double dbl;
if ( double .TryParse(text,NumberStyles.Float | NumberStyles.AllowThousands,culture, out dbl))
{
text = dbl.ToString( N2,culture);
}
return text;
}
Hi,
I am trying to convert a numeric value (Actually from a string variable i.e., "500000.000000000000") in a IValueConverter when my machines currency format was set to Spanish(Chile) in Region settings dialog which results in strange value something like "500,000,000,000,000,000.00" i don't know what actually wrong in my code.
My IValueConverter Converter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return string.Empty;
string text = value.ToString();
double dbl = 0.0;
if (double.TryParse(text, out dbl))
text = dbl.ToString("N2", CultureInfo.InstalledUICulture);
return text;
}
Any guess why am getting this strange value?
How about something like this instead:
<TextBox Text="{Binding Value, StringFormat=N2}" /> <TextBox Text="{Binding Value, StringFormat={}{0:#,#.00}}" />
When you don't pass anIFormatProvider
to theTryParse
method, it defaults to using CultureInfo.CurrentCulture[^]. By default, this uses the current regional settings - I suspect that "Spanish (Chile)" uses.
as the thousands separator, and,
as the decimal separator, so the value is parsed as500000000000000000
.
You're then using the InstalledUICulture[^] to format the value. This is the culture installed with the OS, and will not reflect the user's regional settings. This is probably set to "English (US)", which uses,
as the thousands separator, and.
as the decimal separator.
(There's a good description of the different culture options on StackOverflow[^].)
To get consistent results, you need to use the same format provider to parse and format your values. There is aCultureInfo
parameter passed in to theConvert
method, so you should probably use that:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return string.Empty; string text = value.ToString(); double dbl; if (double.TryParse(text, NumberStyles.Float | NumberStyles.AllowThousands, culture, out dbl)) { text = dbl.ToString("N2", culture); } return text; }
这篇关于使用IValueConverter进行货币转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!