在不同的文化格式化数字 [英] Formatting numbers in different cultures

查看:262
本文介绍了在不同的文化格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个不变的文化,是可以定义的格式不同的组分隔符 - ?比逗号

Assuming an invariant culture, is it possible to define a different group separator in the format - than the comma?

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
Console.WriteLine(String.Format("{0:#,##0}", 2295));

输出:

2,295

所需的输出:

Desired output:

2.295

在不变的文化是一个要求,因为从很多不同的语言环境中的货币被格式化的格式化字符串,对已用户自定义。如丹麦,他们所定义的价格格式为{0:0} - ,而爱尔兰也可能是€{0:#,## 0}。

The invariant culture is a requirement because currencies from many different locales are being formatted with format strings, that have been user defined. Ie for Denmark they have defined the price format to be "{0:0},-", while for Ireland it might be "€{0:#,##0}".

推荐答案

当你有不同的格式字符串,这并不意味着你必须使用InvariantCulture的。如果你有一个格式字符串德国例如您使用格式化的文化(反德),此字符串:

When you have different format strings, this does not mean that you have to use InvariantCulture. If you have a format string for germany e.g. you format this string using the Culture("de-de"):

String.Format(CultureInfo.GetCultureInfo( "de-de" ), "{0:0},-", 2295) //will result in 2.295,-
String.Format(CultureInfo.GetCultureInfo( "en-us" ), "{0:0},-", 2295) //will result in 2,295,-

另外,您可以指定您的自定义<一个href="http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo%28v=VS.100%29.aspx">number格式信息:

NumberFormatInfo nfi = new NumberFormatInfo( )
{
    CurrencyGroupSeparator = ":"
};
String.Format(nfi, "{0:0},-", 2295) //will result in 2:295,-

这篇关于在不同的文化格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆