How to Specify the Number of Decimal Places for a Number in C# ?

Sometimes , you might want to specify the maximum number of decimal places that will be displayed to the user . For example , assume that the input value is 7890.3456 and if you want to display only 7890.34 (Note : only 2 decimal places) , you can use the format specifier “F2” to display 2 decimal places.

How to Specify the Number of Decimal Places for a Number in C# ?

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Ginktage
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            double InputValue = 7890.3456;
            Console.WriteLine(InputValue.ToString("F2", CultureInfo.InvariantCulture));
            Console.ReadLine();
        }
    }
    
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...