我的項目中有很多地方嘗試使用內置的{0:C}
貨幣格式顯示貨幣。如果數字是負數,則它將括在圓括號中。我希望它使用負號代替。我可以獲得標準貨幣格式以使用負號而不是括號嗎?
我的web.config文化設置爲auto
,它解析爲en-US
。
理想的解決方案是使用{0:C}
顯示en-US
文化的負面信號的全局web.config或其他設置,但我也接受其他不太好的解決方案。
我的項目中有很多地方嘗試使用內置的{0:C}
貨幣格式顯示貨幣。如果數字是負數,則它將括在圓括號中。我希望它使用負號代替。我可以獲得標準貨幣格式以使用負號而不是括號嗎?
我的web.config文化設置爲auto
,它解析爲en-US
。
理想的解決方案是使用{0:C}
顯示en-US
文化的負面信號的全局web.config或其他設置,但我也接受其他不太好的解決方案。
我想答案的組合在這裏會讓你更接近你想要的東西。
protected void Application_BeginRequest()
{
var ci = CultureInfo.GetCultureInfo("en-US");
if (Thread.CurrentThread.CurrentCulture.DisplayName == ci.DisplayName)
{
ci = CultureInfo.CreateSpecificCulture("en-US");
ci.NumberFormat.CurrencyNegativePattern = 1;
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
}
如果你想沒有與這樣一個文化涉及任何代碼...我相信你需要建立自己的文化...... Check this Question
據我瞭解你的問題。
您想根據文化顯示貨幣格式。
每次你做文化特定的事情,.NET看起來Thread.CurrentThread.CurrentCulture
和Thread.CurrentThread.CurrentUICulture
。
您可以使用global.asax BeginRequest
方法在ASP.NET中設置所需的文化。
protected void Application_BeginRequest()
{
var ci = CultureInfo.GetCultureInfo("en-US"); // put the culture you want in here
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
你必須指定正確的NumberFormatInfo.CurrencyNegativePattern
這可能是1
Decimal dec = new Decimal(-1234.4321);
CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
culture.NumberFormat.CurrencyNegativePattern = 1;
String str = String.Format(culture, "{0:C}", dec);
Console.Write(str);
輸出:
-$1,234.43
的[全球變化的格式可能重複在C#中負的貨幣數量](http://stackoverflow.com/questions/3994601/globally-changing-format-of-negative-currency-numbers-in-c-sharp) – jmort253 2012-07-28 23:29:52