Decimal.ToString() with currency symbol

Decimal.ToString() with currency symbol

Have you ever wondered how you can edit the “currency appearance” in the .Net ToString() method? Let’s say we have var amount = 1337.42M; and we want it to be displayed like: 1'337.42 CHF.

Nothing easier than that you might think. Just write: amount.ToString("c"); and you are fine. But what is the output of our statement above (depending on your locale CultureInfo)? In my case it was: Fr. 1'337.42.

Changing the statement to amount.ToString("c", new CultureInfo("de-CH")); didn’t change anything.

After looking for the decimal.ToString() method in the MSDN docs, I found the right solution. I had to use the Decimal.ToString(String, IFormatProvider) method.

After using my custom NumberFormatInfo it was easy to display the number in the right format:

And the output was: 1'337.42 CHF.

Thomas Bandixen's Picture

About Thomas Bandixen

Thomas Bandixen is a senior consultant, Microsoft Certified Trainer (MCT) and speaker working for the swiss IT service provider Trivadis AG. His primary focus is on modern web architectures using Angular, TypeScript, HTML5, CSS and ASP.NET Core.

Switzerland https://thomasbandixen.ch