不同国家的货币格式

New*_*wie 4 .net c# xaml xamarin.forms

我正在使用 Xamarin Forms 并尝试为以下形式的货币格式化字符串(在 XAML 视图中):1.235.436,00其中逗号,是小数点分隔符。起初,我使用StringFormat{0:C2}和结果是​​:$1,235,436.00这是为美国。之后,我尝试了StringFormat{0:#,0.#0},结果:1,235,436.00

最后,我尝试通过交换点和逗号来使用相同的逻辑{0:#.0,#0},我得到了结果:1235436.000

那么我如何格式化法国、西班牙、瑞典等的货币格式0.000.000,00 €/kr呢?

XAML 代码:

Label Text="{Binding value, StringFormat='{0:C2}.'}"
Run Code Online (Sandbox Code Playgroud)

在我的情况下这是不正确的。

Mar*_*und 7

您不能直接在绑定中使用StringFormat. 您可以实现自定义IValueConverter或创建string属性,然后在代码中实现格式:

1235436.ToString("C2", CultureInfo.CreateSpecificCulture("es-ES"));
Run Code Online (Sandbox Code Playgroud)

这会将数字格式化为具有特定西班牙文化显示设置的货币。

您还可以修改默认区域性格式以强制使用特定格式:

var culture = CultureInfo.CreateSpecificCulture("en-US");
culture.NumberFormat.CurrencyDecimalSeparator = ",";
culture.NumberFormat.CurrencyGroupSeparator = ".";
Run Code Online (Sandbox Code Playgroud)

但是您不需要这样做。区域性默认值以用户期望显示内容的方式设置。默认格式的更改可能会导致误解。