我有一個問題。如果小於1,則無法找到不帶前導零的情況下將小數轉換爲貨幣字符串的格式。.NET貨幣字符串抑制前導零,如果值<1
例如,
decimal d = 0.14M;
d.ToString("C"); // Translates to $0.14 But I need to get $ .14
是否有一些特殊的精度說明符來豐富這種效果,只是調用ToString?
我有一個問題。如果小於1,則無法找到不帶前導零的情況下將小數轉換爲貨幣字符串的格式。.NET貨幣字符串抑制前導零,如果值<1
例如,
decimal d = 0.14M;
d.ToString("C"); // Translates to $0.14 But I need to get $ .14
是否有一些特殊的精度說明符來豐富這種效果,只是調用ToString?
試試這個:
string result = String.Format("{0:#.0}", d);
decimal d = 0.14M;
var cs = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
string result = String.Format(String.Concat("{0:", cs, " #.00}"), d);
// result == "$ .14" if CurrentCulture is "en-US"
此代碼假定貨幣符號始終放在由空格分隔的數字的前面。這不適用於所有文化,例如'12.34M.ToString(「C」,新的CultureInfo(「ru」))'返回'12,34р.'。 – 2012-02-09 15:27:17
嗯..我認爲這是光明的決定,但我看到 – snayko 2012-02-09 15:36:33
@Martin Liversage我認爲這是OP想要的。另外,您的解決方案在貨幣符號和小數之間沒有空格。 – 2012-02-09 17:18:57
如果你想利用文化考慮您需要使用NumberFormatInfo
提供的信息。以下是將小數格式設置爲貨幣但不包含前導零的擴展方法。如果您不想使用擴展方法,則可以輕鬆地將擴展方法更改爲常規方法。
代碼中有一個「快捷方式」。它無法處理多個組大小。我不認爲這可以使用自定義數字格式完成,唯一可行的解決方案將使用N
進行格式化,並刪除任何領先的0
。
static class DecimalExtensions {
public static String ToCurrencyFormat(this Decimal value) {
return ToCurrencyFormat(value, CultureInfo.CurrentCulture);
}
public static String ToCurrencyFormat(this Decimal value, CultureInfo cultureInfo) {
return ToCurrencyFormat(value, cultureInfo.NumberFormat);
}
public static String ToCurrencyFormat(this Decimal value,
NumberFormatInfo numberFormat) {
// Assume the CurrencyGroupSizes contains a single element.
var format =
"#,"
+ new String('#', numberFormat.CurrencyGroupSizes[0])
+ "."
+ new String('0', numberFormat.CurrencyDecimalDigits);
var formattedValue = Math.Abs(value).ToString(format, numberFormat);
if (value >= Decimal.Zero)
return FormatPositiveCurrency(
numberFormat.CurrencyPositivePattern,
numberFormat.CurrencySymbol,
formattedValue
);
else
return FormatNegativeCurrency(
numberFormat.CurrencyNegativePattern,
numberFormat.CurrencySymbol,
formattedValue
);
}
static String FormatPositiveCurrency(Int32 pattern, String symbol, String value) {
switch (pattern) {
case 0:
return symbol + value;
case 1:
return value + symbol;
case 2:
return symbol + " " + value;
case 3:
return value + " " + symbol;
default:
throw new ArgumentException();
}
}
static String FormatNegativeCurrency(Int32 pattern, String symbol, String value) {
switch (pattern) {
case 0:
return "(" + symbol + value + ")";
case 1:
return "-" + symbol + value;
case 2:
return symbol + "-" + value;
case 3:
return symbol + value + "-";
case 4:
return "(" + value + symbol + ")";
case 5:
return "-" + value + symbol;
case 6:
return value + "-" + symbol;
case 7:
return value + symbol + "-";
case 8:
return "-" + value + " " + symbol;
case 9:
return "-" + symbol + " " + value;
case 10:
return value + " " + symbol + "-";
case 11:
return symbol + " " + value + "-";
case 12:
return symbol + " -" + value;
case 13:
return value + "- " + symbol;
case 14:
return "(" + symbol + " " + value + ")";
case 15:
return "(" + value + " " + symbol + ")";
default:
throw new ArgumentException();
}
}
}
哪個不會給你當前文化的貨幣符號。 – Oded 2012-02-09 13:48:12
正確,必須在某處添加「C」:) – 2012-02-09 13:51:48