Reputation: 1926
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.
I have this:
<span>
<b>Price: </b><%#:String.Format(new System.Globalization.CultureInfo("sw-KE"), "{0:c}", Item.BeatPrice)%>
</span>
Culture name sourced from http://msdn.microsoft.com/en-us/library/system.globalization.cultureinfo%28v=vs.80%29.aspx.
However, the price shows as S3,000
instead of KES 3,000
.
What do I need to do to format the price correctly?
Upvotes: 5
Views: 1909
Reputation: 2484
While using String.Format "c" or "C" gives you the currency symbol for specified culture. You are trying to show Currency ISO code for Kenya Shillings. Below code will display exactly what you wanted.
String.Format("{0} {1}", (new RegionInfo("sw-KE")).ISOCurrencySymbol, Item.BeatPrice)
If you don't change culture on your application easy way to do this.
String.Format("{0} {1}", "KES", Item.BeatPrice)
Upvotes: 1
Reputation: 153
It's better not to hardcode the CurrencySymbol, so you should use
var regionInfo = new RegionInfo("sw-KE");
var currencySymbol = regionInfo.ISOCurrencySymbol;
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();
}
Which gives you a formatted currency string based on the current UICulture.
Upvotes: 3
Reputation: 7724
Like Ondrej Svejdar said, there are two symbols, as in $ vs. USD:
var region = new System.Globalization.RegionInfo("sw-KE");
Console.WriteLine(region.CurrencySymbol); // "S"
Console.WriteLine(region.ISOCurrencySymbol); // "KES"
Note: When I ran this on IDEone (which compiles with Mono), the results were unexpected ("KES" and "Kenyan Shilling").
Upvotes: 1
Reputation: 3297
The best practice is to format the String to Currency format {0:C}
and change the current thread UICulture or Culture to KES, and ASP.NET is smart enough to display the page accroding to your currunt culuture.
Note: You can change culture by changing the culture of the browser (you can to do this for development purposes) but best practice change the culture programmatically, for example here I'm change the culture base on user Cookie and mu default culture is en-us like this.
protected override void InitializeCulture()
{
HttpCookie cultureCookie = Request.Cookies["culture"];
if (cultureCookie == null)
{
cultureCookie = new HttpCookie("culture", "en-US");
Response.Cookies.Add(cultureCookie);
}
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureCookie.Value);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureCookie.Value);
base.InitializeCulture();
}
Upvotes: 0
Reputation: 1515
If your machine's regional settings are properly set then you can use:
Console.WriteLine(string.Format(CultureInfo.InvariantCulture, "{0:c}", Item.BeatPrice));
It will automatically take culture based on your machine's regional settings.
Upvotes: 1
Reputation: 7301
If the format is not as you expect you can add custom string formatting:
String.Format("KES {0:N3}", Item.BeatPrice)
Hope this works.
Upvotes: 4