Formatting Decimals in C#

by Dave on September 23, 2009

in String Operations

In this post I am going to show you a few different ways how you can format a decimal number (float, double, or decimal).

Setting the Maximum Allowed Decimal Places

To format your numbers to a maximum of two decimal places use the format string {0:0.##} as shown in the below example:

string.Format("{0:0.##}", 256.583); // "256.58"
string.Format("{0:0.##}", 256.586); // "256.59"
string.Format("{0:0.##}", 256.58);  // "256.58"
string.Format("{0:0.##}", 256.5);   // "256.5"
string.Format("{0:0.##}", 256.0);   // "256"

Setting a Fixed Amount of Decimal Places

This is similar to the above example but instead of hashes (‘#’) in our format string we are going to use zeroes (’0′) as shown below:

string.Format("{0:0.00}", 256.583); // "256.58"
string.Format("{0:0.00}", 256.586); // "256.59"
string.Format("{0:0.00}", 256.58);  // "256.58"
string.Format("{0:0.00}", 256.5);   // "256.50"
string.Format("{0:0.00}", 256.0);   // "256.00"

The Thousand Separator

To format your decimal number using the thousand separator, use the format string {0:0,0} as shown in the below example:

string.Format("{0:0,0.00}", 1234256.583); // "1,234,256.58"
string.Format("{0:0,0}", 1234256.583);    // "1,234,257"

Setting a Fixed Amount of Digits Before the Decimal Point

To set a minimum amount of three digits before the decimal point use the format string {0:000.##}.

string.Format("{0:00.000}", 1.2345);    // "01.235"
string.Format("{0:000.000}", 12.345);   // "012.345"
string.Format("{0:0000.000}", 123.456); // "0123.456"

Alignment

To specify alignment to the Format method you must write your format string as shown below. Note we are using a comma (‘,’) to specify the number of characters used for alignment.

{0,[no. of chars]} and if you want to pad with zeroes {0,[no. of chars]:00.00}

string.Format("{0,7:##.00}", 2.356);  // "   2.36"
string.Format("{0,-7:##.00}", 2.356); // "2.36   "
string.Format("{0,7:00.00}", 2.356);  // "  02.36"
string.Format("{0,-7:00.00}", 2.356); // "02.36  "

Positive Numbers, Negative Numbers, and Zero

You can include different formats for positive numbers, negative numbers, and zero by using the semicolon character (‘;’).

Format string:
{0:[positive];[negative];[zero]}

string.Format("{0:000.000;(000.000);zero}", 23.43);  // "023.430"
string.Format("{0:000.000;(000.000);zero}", -23.43); // "(023.430)"
string.Format("{0:000.000;(000.000);zero}", 0.0);    // "zero"

Some Pre-Defined Formats

string.Format("{0:C}", 1532.236);  // "£1,532.24"
string.Format("{0:C}", -1532.236); // "-£1,532.24"
string.Format("{0:E}", 1532.236);  // "1.532236E+003"
string.Format("{0:E}", -1532.236); // "-1.532236E+003"
string.Format("{0:F}", 1532.24);   // "1532.24"
string.Format("{0:F}", -1532.24);  // "-1532.24"
string.Format("{0:G}", 1532.236);  // "1532.236"
string.Format("{0:G}", -1532.236); // "-1532.236"
string.Format("{0:N}", 1532.236);  // "1,532.24"
string.Format("{0:N}", -1532.236); // "-1,532.24"
string.Format("{0:P}", 0.1532);    // "15.32 %"
string.Format("{0:P}", -0.1532);   // "-15.32 %"
string.Format("{0:R}", 1532.236);  // "1532.236"
string.Format("{0:R}", -1532.236); // "-1532.236"

Happy formatting. :)
Dave



{ 5 comments… read them below or add one }

George October 26, 2012 at 01:33

About the post “Formatting Decimals in C#”. Excellent examples David!

Only one question that I cant understand what it means. Fallowing the example:

string.Format(“{0:0.00}”, 256.583); // “256.58″

What means in a format string the “:” sign? Because according to your post there are examples where you use the format “0:” and in other examples “7:”

Thanks in advance

George

Reply

Dave October 31, 2012 at 13:33

Hi George,

The ':' in a format string is the seperator between the value placeholder and the value’s format. So in the below example the first placeholder '{0}' is just going to display the first parameter which is the day of the week – note there is no formatting here, and the second placeholder '{1:t}' is going to hold the second parameter which is the time, and will also format it using the value 't' which stands for Short time pattern. More date and time format strings can be found here.

string.Format("Today is {0} and it's currently {1:t}.", DateTime.Now.DayOfWeek, DateTime.Now);

This will display as Today is Wednesday and it’s currently 1:28 PM.

As for your other question regarding the below example – the '7' means that the formatted result is padded with spaces until it reaches a maximum of 7 characters long.

string.Format("{0,7:##.00}", 2.356); // " 2.36"

Hope this clarifies things a bit for you.

Reply

Jesus Carrillo April 18, 2013 at 11:16

Hello,

I hope you can help me whit this problem.

I am trying to delete decimal point with String.Format.

For example, if I have 123.45 I want to get 12345, is there a way to do this with String Formatting? I know it will be easier to do it by replace o anything else, but I really really need to do it with String.Format (Visual Basic)

Thank you for your help

Reply

Dave April 18, 2013 at 18:54

I don’t think you can do what you want with string.Format, and even if there is a way I don’t think that it’s the clearest way to do it. I would use a replace or trim method instead.

Cheers,
Dave

Reply

Jesus Carrillo April 20, 2013 at 05:56

Thank you Dave for your answer

I had to change a little the logic on the app to acomplish it.

Again, thank you for your answer.

Reply

Leave a Comment

{ 1 trackback }

Previous post:

Next post: