Formatting Decimals in C#

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"


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:

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 runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

10 comments… add one
  • George

    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


    • 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.

  • Jesus Carrillo


    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

    • 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.


      • Jesus Carrillo

        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.



    I need your help to convert decimal number witn 3 digit after the comma and with space thousands separator. i develop an application with VS 2012 C# and WPF.

    forgive my english is not good,

    Thank you in advance,

  • Jayateerth

    Hi Dave ,

    Below number i cant able to convert to string

    float f2 = 1.2788800E7f;

    • Try f2.ToString("F"); where ‘F’ stands for ‘Fixed-Point’ formatting. It should give you 12788800.00.

Leave a Comment