Wednesday, 13 October 2010

C# Code to Print Maximum Two Decimal Places

Steps to print maximum two decimal places:

1. Use {0:0.##} to print two or less than two decimal places.

2. Either use with String.Format() or WriteLine().

C# Code for Max Two Decimal Places:

using System;
using System.Text;

namespace TestConsole
{
    class MaxTwoDecimal
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:0.##}", 2.6243));
            Console.WriteLine(String.Format("{0:0.##}", -7353.20));
            //or
            Console.WriteLine("{0:0.##}", 5010);
            Console.ReadLine();
        }
    }
}

 

Output:

C# Code to Print Maximum Two Decimal Places

No comments:

Post a Comment