Format Currency Sign on Charts, Grids and Pages


It is often then you develop a web page in ASP.NET and you are required to show custom culture information.
But often the data has only one(same) currency(for example Swedish Krona).

A nice and easy solution is to use Thread.CurrentThread.CurrentCulture property. Here is a sample of code. The advantage is that you can have it in one place and the whole application will use it.

//Try to get user's browser predefined language, if fails go to English as default language
var userLanguage = Request.UserLanguages.FirstOrDefault() ?? "en";
var userCulture = CultureInfo.CreateSpecificCulture(userLanguage);
//Set number format for userCulture to swedish(override current user culture)
//So we can have kr as a currency sign.
NumberFormatInfo nfiSwedish = new CultureInfo("sv-SE", false).NumberFormat;
userCulture.NumberFormat.CurrencySymbol = nfiSwedish.CurrencySymbol + " ";

Thread.CurrentThread.CurrentCulture = userCulture;
Thread.CurrentThread.CurrentUICulture = userCulture;

As the result, we could see the interface in English(like dates, captions, etc), while the numbers are formatted by using US standard, but with kr sign.

Next thing you should do sometimes is set the format of the output string. This is usually done by using String.Format. Here I will show how to set currency format by using design time editor for amazing and free Microsoft Chart Control. This is done as shown on the picture below:

Comments

  1. Metalda, your post looks like a f**ing spam. Isn't it?

    I remember I used to write following line in my applications at University:
    System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

    for being able to read numbers with dot separators. Kinda different computers had different Cultures.

    ReplyDelete

Post a Comment

Popular posts from this blog

UML Sequence Diagram and Visual Studio

Compare .NET objects or Why Assert.AreEqual fails for complex objects.