問題描述
考慮一個(gè)十進(jìn)制
值:
Decimal value = -1234567890.1234789012M;
我想將此十進(jìn)制
值轉(zhuǎn)換為字符串,并包含千位分隔符".
i want to convert this Decimal
value to a string, and include "thousands separators".
注意:我不想包含千位分隔符,我想包含數(shù)字分組.對(duì)于不將數(shù)字分組為數(shù)千或不使用逗號(hào)分隔組的文化,這種差異很重要
Note: i don't want to include thousand's separators, i want to include digit grouping. The difference is important for cultures that don't group numbers into thousands, or don't use commas to separate groups
在我的計(jì)算機(jī)上使用我當(dāng)前的語言環(huán)境使用不同標(biāo)準(zhǔn)格式字符串的一些示例輸出:
Some example output with different standard formatting strings, on my computer, with my current locale:
value.ToString() = -1234567890..1234789012 (Implicit General)
value.ToString("g") = -1234567890..1234789012 (General)
value.ToString("d") = FormatException (Decimal whole number)
value.ToString("e") = -1..234568e++009 (Scientific)
value.ToString("f") = -1234567890..123 (Fixed Point)
value.ToString("n") = -12,,3456,,7890..123 (Number with commas for thousands)
value.ToString("r") = FormatException (Round trippable)
value.ToString("c") = -$$12,,3456,,7890..123 (Currency)
value.ToString("#,0.#") = -12,,3456,,7890..1
我想要(取決于文化)是:
en-US -1,234,567,890.1234789012
ca-ES -1.234.567.890,1234789012
gsw-FR -1 234 567 890,1234789012 (12/1/2012: fixed gws-FR to gsw-FR)
fr-CH -1'234'567'890.1234789012
ar-DZ 1,234,567,890.1234789012-
prs-AF 1.234.567.890,1234789012-
ps-AF 1?234?567?890,1234789012-
as-IN -1,23,45,67,890.1234789012
lo-LA (1234567,890.1234789012) (some debate if numbers should be "1,234,567,890")
qps-PLOC 12,,3456,,7890..1234789012
如何將 Decimal
轉(zhuǎn)換為帶有數(shù)字分組的字符串?
How can i convert a Decimal
to a string, with digit groupings?
更新:更多期望輸出,使用我目前的文化:
Update: Some more desired output, using my current culture of :
-1234567890M --> -12,,3456,,7890
-1234567890.1M --> -12,,3456,,7890..1
-1234567890.12M --> -12,,3456,,7890..12
-1234567890.123M --> -12,,3456,,7890..123
-1234567890.1234M --> -12,,3456,,7890..1234
-1234567890.12347M --> -12,,3456,,7890..12347
-1234567890.123478M --> -12,,3456,,7890..123478
-1234567890.1234789M --> -12,,3456,,7890..1234789
-1234567890.12347890M --> -12,,3456,,7890..1234789
-1234567890.123478901M --> -12,,3456,,7890..123478901
-1234567890.1234789012M --> -12,,3456,,7890..1234789012
<小時(shí)>
更新:我嘗試查看 Decimal.ToString()
如何設(shè)法使用 General 格式來顯示它需要的所有數(shù)字顯示:
Update: i tried peeking at how Decimal.ToString()
manages to use the General format to show all the digits that it needs to show:
public override string ToString()
{
return Number.FormatDecimal(this, null, NumberFormatInfo.CurrentInfo);
}
除了 Number.FormatDecimal
隱藏在某處:
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern string FormatDecimal(decimal value, string format, NumberFormatInfo info);
所以這是一條死胡同.
推薦答案
您可以指定自定義模式(該模式將適當(dāng)?shù)亟馕鰹樘囟ㄓ谖幕姆纸M方法以及適當(dāng)?shù)姆纸M和小數(shù)分隔符).一個(gè)模式可以有正、負(fù)和零部分.積極模式始終相同,但消極模式取決于文化,可以從 NumberFormatInfo 的 NumberNegativePattern 屬性.由于您希望盡可能地精確,因此需要在小數(shù)點(diǎn)后填寫 28 位占位符;逗號(hào)強(qiáng)制分組.
You can specify a custom pattern (the pattern will appropriately resolve to the culture specific method of grouping and the appropriate grouping and decimal separator characters). A pattern can have positive, negative and zero sections. The positive pattern is always the same but the negative pattern depends on the culture and can be retrieved from the NumberFormatInfo's NumberNegativePattern property. Since you want as much precision as possible, you need to fill out 28 digit placeholders after the decimal; the comma forces grouping.
public static class DecimalFormatters
{
public static string ToStringNoTruncation(this Decimal n, IFormatProvider format)
{
NumberFormatInfo nfi = NumberFormatInfo.GetInstance(format);
string[] numberNegativePatterns = {
"(#,0.############################)", //0: (n)
"-#,0.############################", //1: -n
"- #,0.############################", //2: - n
"#,0.############################-", //3: n-
"#,0.############################ -"};//4: n -
var pattern = "#,0.############################;" + numberNegativePatterns[nfi.NumberNegativePattern];
return n.ToString(pattern, format);
}
public static string ToStringNoTruncation(this Decimal n)
{
return n.ToStringNoTruncation(CultureInfo.CurrentCulture);
}
}
樣本輸出
Locale Output
======== ============================
en-US -1,234,567,890.1234789012
ca-ES -1.234.567.890,1234789012
hr-HR - 1.234.567.890,1234789012
gsw-FR -1?234?567?890,1234789012
fr-CH -1'234'567'890.1234789012
ar-DZ 1,234,567,890.1234789012-
prs-AF 1.234.567.890,1234789012-
ps-AF 1?234?567?890,1234789012-
as-IN -1,23,45,67,890.1234789012
lo-LA (1234567,890.1234789012)
qps-PLOC -12,,3456,,7890..1234789012
目前沒有使用 NegativeNumberFormat
4 (n -
) 的語言環(huán)境,因此無法測(cè)試這種情況.但沒有理由認(rèn)為它會(huì)失敗.
There is currently no locale that uses NegativeNumberFormat
4 (n -
), so that case cannot be tested. But there's no reason to think it would fail.
這篇關(guān)于十進(jìn)制字符串與千位分隔符?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!