{%Pragmatic Coding%} Tips and tricks for software developers.


How To: Format Decimal.ToString() in C#

Today I have spent some time to find out how correctly convert and format decimal number to string representation with 2 decimal digits. And here is what I found and learned I want to share it with you my friend.
I'll give you several C# examples that shows how to format float numbers to string in C#. You can use static method String.Format or instance methods double.ToString and float.ToString.

Digits after decimal point

This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.

Next example formats double to string with floating number of decimal places. E.g. for maximal two decimal places use pattern „0.##“.

Digits before decimal point

If you want a float number to have any minimal number of digits before decimal point use N-times zero before decimal point. E.g. pattern „00.0“ formats a float number to string with at least two digits before decimal point and one digit after that.

Thousands separator

To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.


Float numbers between zero and one can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use # before point. For example „#.0“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).

Following code shows how can be formatted a zero (of double type).

Align numbers with spaces

To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not in double.ToString method). To align numbers to the left use negative number of spaces.

Custom formatting for negative numbers and zero

If you need to use custom format for negative float numbers or zero, use semicolon separator „;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section.

Some funny examples

As you could notice in the previous example, you can put any text into formatting pattern, e.g. before an usual pattern „my text 0.0“. You can even put any text between the zeroes, e.g. „0aaa.bbb0“.

And finally really good examples of trailing zeros.

Display percentage string representation of a decimal

Also I spend a lot of time on trying to display a percentage – string representation of a decimal but without trailing zeros, e.g. 15.1% instead of 15.10%.

In the application I already had  NumberFormatInfo for displaying monetary values and percentages. An object of that type was defined as follows:

Let’s take the following decimals as examples:

Having nfi I could get a string representation of a decimal as below:

As you see nfi was defined so that only two decimal places were displayed, but always, disregarding the fact whether they are zeros or not. I tried to find a member of NumberFormatInfo class, a hint of how to use it for that purpose (including parsing the string as a result of ToString with passing nfi), but unfortunately fail.
But I'm asked my self why not to use decimal.ToString() without an instance of NumberFormatInfo class.
And here is what I got.

It’s worth mentioning that weren’t there ‘%’ character in the expression passed to code>ToString method I would first have to multiply the decimals by 100:

That’s it.

Thanks for your patience. I hope this article was helpful for you!

Share on social network

Share to Facebook
Share to Google Plus
Share to LiveJournal
Share to MyWorld
Share to Odnoklassniki
Share to Yandex
Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.