Wednesday, 13 October 2010

How to Format Number to Two Decimal places using C#

Programming Steps:

1. Either you can use String.Format() function to format in given format.

2. Or you can directly use Console.WriteLine("{0:0.00}", 100.0) to print number in a format.

Output of program:

How to Format Number to two Decimal places

Code for Formatting number to two decimal places:

using System;
using System.Text;

namespace TestConsole
{
    class DecimalFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:0.00}", 163.48967));
            Console.WriteLine(String.Format("{0:0.00}", 173.4));
            Console.WriteLine(String.Format("{0:0.00}", 193.0));

            //or
            Console.WriteLine("{0:0.00}", 100.0);

            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment