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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...