Monday, August 18, 2008

Currency format in method ToString()

As it is documented in msdn documentation when you want to display currency value from some integer, decimal or any other numeric type you can use method ToString() with currency sign ("C") as a method parameter. Based on current culture info assigned to thread ToString method returns well formatted string. But what if you need some sort of customization of formatted value?

For example, the issue I currently working on.

Culture Info: de-CH (swiss german)

int price = 26540

Method price.ToString("C") returns CHF 26'540.00

This is cool and nice but I need a value without decimal places. Searching thorugh documentation and some forums I found it myself. When you won't decimal places use another parameter for ToString() method - "C0" (c zero);


So, solution is price.ToString("C0") . It returns CHF 26'540 without decimal places.

Cheers

1 comment:

Anonymous said...

Thanks for writing this.