如何在.NET中将整数转换为本地化字符串?
Exemptigratia:
Int64 value = 12345678901;
value.ToString();
// returns "12345678901", should be "123,4567,8901"
value.ToString(CultureInfo.CurrentCulture);
// returns "12345678901", should be "123,4567,8901"
value.ToString(CultureInfo.CreateSpecificCulture("en-US"))
// returns "12345678901", should be "12,345,678,901"
value.ToString(CultureInfo.CreateSpecificCulture("fr-CH"))
// returns "12345678901", should be "12'345'678'901"
value.ToString(CultureInfo.CreateSpecificCulture("te-IN"))
// returns "12345678901", should be "12,34,56,78,901"
如何在.NET中将整数(或浮点数)转换为本地化字符串?
世界上唯一的数字格式列表:
可在.NET中找到这些文化设置的其他奖励信息:
CultureInfo.CreateSpecificCulture("en-US").NumberFormat.NumberGroupSizes = { 3 }
CultureInfo.CreateSpecificCulture("en-US").NumberFormat.NumberGroupSeparator = ","
CultureInfo.CreateSpecificCulture("te-IN").NumberFormat.NumberGroupSizes = { 3, 2 }
CultureInfo.CreateSpecificCulture("te-IN").NumberFormat.NumberGroupSeparator = ","
CultureInfo.CreateSpecificCulture("te-IN").NumberFormat.NumberGroupSizes = { 3 }
CultureInfo.CreateSpecificCulture("te-IN").NumberFormat.NumberGroupSeparator = "'"
答案 0 :(得分:9)
使用精度为0的“N”standard format specifier。
using System;
class Test
{
static void Main()
{
long value = 12345678901;
string text = value.ToString("N0");
Console.WriteLine(text);
}
}
结果(在我的英国机器上):
12,345,678,901
这将考虑到当前文化的各个方面。当然,您可以单独指定文化。
修改:特定文化的广义形式:
value.ToString("N0", CultureInfo.CreateSpecificCulture("fr-CH"));