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();
        }
    }
    
}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply


x

Interesting BlogPosts to read

C# Program to Display the Reverse of a Number
Introduction This program in C# developed using .NET Framework , C# and Visual Studio  will demonst...
Finding the Reverse of a Number in C# using Extension Methods
If you are looking forward to find the reverse of a number in C# , below is a sample source code tha...
Which port number does SQL Server run on?
Which port number does SQL Server run on? By default, Microsoft SQL Server runs on port 1433.If...