字符串格式化为货币C# [英] String Formatting to Currency C#
问题描述
我是新来的建设ASP.NET Web应用程序和想显示肯尼亚先令的货币。对于先令的符号是KES。
I am new to building web applications in ASP.NET and am trying to display a currency in Kenya Shillings. The symbol for the shilling is KES.
我有这样的:
<span>
<b>Price: </b><%#:String.Format(new System.Globalization.CultureInfo("sw-KE"), "{0:c}", Item.BeatPrice)%>
</span>
文化名来自的http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.80%29.aspx.
不过,价格显示为 S3,000
而不是 KES 3000
。
However, the price shows as S3,000
instead of KES 3,000
.
什么我需要做的正确格式化价格是多少?
What do I need to do to format the price correctly?
推荐答案
这是最好不要硬code中的CURRENCYSYMBOL,所以你应该使用
It's better not to hardcode the CurrencySymbol, so you should use
var regionInfo = new RegionInfo("sw-KE");
var currencySymbol = regionInfo.ISOCurrencySymbol;
要得到正确的CURRENCYSYMBOL为你的文化。
to get the correct CurrencySymbol for your culture.
//编辑:
或者你可以试试这个功能:
//edit: Or you can try this function:
public static string FormatCurrency(decimal value)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentUICulture;
RegionInfo regionInfo = new RegionInfo(cultureInfo.LCID);
string formattedCurrency = String.Format("{0} {1:C}", regionInfo.ISOCurrencySymbol, value);
return formattedCurrency.Replace(cultureInfo.NumberFormat.CurrencySymbol, String.Empty).Trim();
}
,让你根据当前的UICulture格式化货币字符串。
Which gives you a formatted currency string based on the current UICulture.
这篇关于字符串格式化为货币C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!