C# Rounding the digits after decimal point using String.Format() method

C# String.Format() technique example: Here, we will figure out how to adjust the digits after the decimal point utilizing String.Format() strategy in C#?

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Only two decimal points
            Console.WriteLine("Two digits after decimal point");
            Console.WriteLine(String.Format("{0:0.00}", 512.4246));     
            Console.WriteLine(String.Format("{0:0.00}", 512.4));        
            Console.WriteLine(String.Format("{0:0.00}", 512.0));

            Console.WriteLine("\n\nThree digits after decimal point");
            Console.WriteLine(String.Format("{0:0.000}", 512.4246));
            Console.WriteLine(String.Format("{0:0.000}", 512.4));
            Console.WriteLine(String.Format("{0:0.000}", 512.0));

            Console.WriteLine();  
        }
    }
}

To adjust the digits after the decimal point, we can utilize String.Format() technique, here is the example.

Output:

Two digits after decimal point
512.42
512.40
512.00


Three digits after decimal point
512.425
512.400
512.000

Leave a Comment

error: Alert: Content is protected!!