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"

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

19 comments… add one
  • George Link Reply

    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

    • 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 Link Reply

    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

    • 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

      • Jesus Carrillo Link Reply

        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.

    • kk Link Reply

      you should be able to do:
      double number = 123.45;
      string formatted = number.ToString(“0.00”).Replace(“.”, string.Empty);

      but its not elegant

  • G.HATEM Link Reply

    Hello,

    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 Link Reply

    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.

  • Deva Link Reply

    I want to sowing decimals like
    23 (Decimal)
    252

    Please suggest above format how get

  • Pregunton Cojonero Link Reply

    IMHO, it would be very useful (and marvellous) an “Definitive guide” -or all i need know-about decimals-double-float, and formattingdecimals, with Culture, rounding, etc in C#.

    Microsoft has not Definitive guides.

  • Piyush Link Reply

    what can i do for this type format == 1,00,000

  • sumatej Link Reply

    Hi all, Please give me the solution for my below query please guys…

    Actually i have the value “000000099”
    and after some trimming methods the value finally get displayed as “.99”
    Here actually trimming the front all zero’s and displaying from DOT(front all 0’s are removed or trimmed) i.e; finally it displays as “.99”

    So here i need, if the value less than 1 like “0.99” or “0.73” or “0.35”
    it should get displayed as “0.99 or 0.73”
    but not like “.99 or .73”

  • sumatej Link Reply

    Hi Dave,

    My doubt Continues…

    Actually i have the code as:
    strAmount = strAmount.TrimStart (‘0’);
    that’s why its trimming and getting as .99
    I want it to have 0.99 for less than $1 value….

  • sumatej Link Reply

    I think it should be in this format right? or else please suggest DAVE…

    if(strAmount.Equals(“.00”))
    string.Format(“{0:0.00}”, strAmount);

  • Sharada Kailasam Iyer Link Reply

    For example:
    User enters : 0.0 from front end (It should handle all values like 0.00 , 0.000 , 000)
    it should get converted into a single 0. How this can be achieved?
    and then I would like to check if(a == 0)
    {
    then some error
    }

  • Thắng Link Reply

    string.Format(“{0:#,0.###}”, number)
    I used this to my company project. :))
    Thank you for your suggestion

Leave a Comment