.net 如何在“#,##0”和“{0:n2}”样式格式字符串表示之间进行转换?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/569242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert between "#,##0" and "{0:n2}" style format string representations?
提问by Drew Noakes
.NET supports two types of string formatting.
.NET 支持两种类型的字符串格式。
I'm in a situation where existing configuration data has #,##0style formatting. A new feature requires formatting to the same output, but the API needed for this feature only accepts formatting of type {0:n2}.
我处于现有配置数据具有#,##0样式格式的情况。新功能需要格式化为相同的输出,但此功能所需的 API 仅接受类型为 的格式{0:n2}。
Does anyone know of a means to convert between these two representations for numeric types? DateTimecan be ignored.
有谁知道在这两种数字类型表示之间进行转换的方法? DateTime可以忽略。
EDITI've learned that:
编辑我了解到:
The
{0:n2}style is known as standard numeric formattingThe
#,##0style is known as custom numeric formatting
回答by Arjan Einbu
No, you can't.
不,你不能。
From your link to the MSDN articles about standard formatstrings, you'll find:
从指向有关标准格式字符串的 MSDN 文章的链接中,您会发现:
The actual negative number pattern, number group size, thousand separator, and decimal separator are specified by the current NumberFormatInfo object.
实际的负数模式、数字组大小、千位分隔符和小数分隔符由当前 NumberFormatInfo 对象指定。
So the standard formatting specifiers will vary depending on which culture the program is running under.
因此,标准格式说明符将根据程序在哪种文化下运行而有所不同。
Since your custom formating specifies exactly how the number is going to look, whatever the culture the program is running under. Its allways gonna look the same.
由于您的自定义格式准确指定了数字的外观,无论程序在何种文化下运行。它总是看起来一样。
The culture the program is running under isn't known during compile time, it's a runtime property.
程序运行所在的文化在编译时是未知的,它是一个运行时属性。
So the answer is: No, you can't map automatically, because there isn't a one-to-one consistent mapping.
所以答案是:不,你不能自动映射,因为没有一对一的一致映射。
回答by Drew Noakes
HACK ALERT!!!
黑客警报!!!
As Arjan pointed out in his excellent answerwhat I want to do isn't possible in a bullet proof fashion across all locales (Thanks Arjan).
正如Arjan 在他出色的回答中指出的那样,我想做的事情不可能在所有地区都以防弹方式实现(感谢 Arjan)。
For my purposes, I know I'm only dealing with numbers and the major thing I'm concerned with is having the same number of decimal places. So here's my hack.
就我的目的而言,我知道我只处理数字,我关心的主要问题是小数位数相同。所以这是我的黑客。
private static string ConvertCustomToStandardFormat(string customFormatString)
{
if (customFormatString == null || customFormatString.Trim().Length == 0)
return null;
// Percentages do not need decimal places
if (customFormatString.EndsWith("%"))
return "{0:P0}";
int decimalPlaces = 0;
int dpIndex = customFormatString.LastIndexOf('.');
if (dpIndex != -1)
{
for (int i = dpIndex; i < customFormatString.Length; i++)
{
if (customFormatString[i] == '#' || customFormatString[i] == '0')
decimalPlaces++;
}
}
// Use system formatting for numbers, but stipulate the number of decimal places
return "{0:n" + decimalPlaces + "}";
}
回答by Apps Tawale
This used for formatting number into 2 decimal places
这用于将数字格式化为 2 个小数位
string s = string.Format("{0:N2}%", x);

