c sharp

How Do You Round a Number to Two Decimal Places in C#

Rounding is the process of reducing the precision of a number while keeping its value close to the original value. For instance, rounding a number to two decimal places means keeping only the first two digits after the decimal point and discarding the rest. Rounding is a common operation in programming, and C# provides a variety of ways to round numbers.

Using Math.Round()

In C#, this function can be used for rounding a number to a specified number of decimals so to short a number to two decimal places in C# using this function, simply pass the number and the number of decimal places to the method, here is an example code:

using System;

class Program
{
    static void Main(string[] args)
    {
        double num =  3.141592653589793238;
        double roundedNum = Math.Round(num, 2);
        Console.WriteLine(roundedNum);
    }
}

 
In this example, we declare a double variable and give it the value3.141592653589793238” and after that use the Round () function. The result of the rounding operation is stored in a new double variable called roundedNum.

Using Math.Ceiling()

Another way to round a decimal to two places is by using this function you can multiply the number by 100, apply Ceiling() to the result, and then divide by 100 to get the rounded number with two decimal places:

using System;

class Program
{
    static void Main(string[] args)
    {
        double num =  3.141592653589793238;
        double roundedNum = Math.Round(num, 2);
        Console.WriteLine(roundedNum);
    }
}

 
In this example, we declare a double variable and assign it the value of the constant pi for illustration. The result of this operation is stored in a new double variable called roundedNum.

Using Math.floor()

To round a number to two decimal places in C#, using this function, we can multiply the number by 100 and after that apply this function and then divide the result by 100. This will effectively round the number down to two decimal places.

using System;

class Program
{
    static void Main(string[] args)
    {
       
        double num = 3.141592653589793238;
        double roundedNum = Math.Floor(num * 100) / 100;
        Console.WriteLine(roundedNum);
    }
}

 
In this C# code, the Floor() function is used for rounding down the given number  to the nearest integer towards negative infinity and  the result is divided by 100 to shift the decimal point back to its original position, effectively rounding the number to two decimal places.


Note: The main difference between Floor() and Ceiling() is that Floor() always rounds down to the nearest integer, while Ceiling() always rounds up to the nearest integer. Suppose if we apply Floor() to the number 2.99, it will round down to 2. On the other hand, if we apply Math.Ceiling() to the same number, it will round up to 3.

Using String.Format()

To round a number to two decimal places in C# using this function here is an example code that rounds off the value of Pi up to two decimals:

using System;

class Program
{
    static void Main(string[] args)
    {
        double num = 3.141592653589793238;
        string roundedNumber = String.Format("{0:N2}", num);
        Console.WriteLine(roundedNumber); // Output: 15.68
    }
}

 
The code then uses the Format method to round the number to 2 decimal places, storing the result in a string variable named “roundedNumber”. The format string “{0:N2}” tells the method to format the first argument passed to it (in this case, the “num” variable) as a number with two decimal places.

In the code “{0:N2}”, “{0}” represents the position of the parameter in the argument list, and “N2” specifies the number format string.

The “N” format specifier is used to format a number as a string with a specific number of decimal places. In this case, “N2” means that the number should be formatted with two decimal places.

Conclusion

Rounding a number to two decimal places is a common task in programming, and there are several methods available to accomplish this in C#. In this article, we’ve covered four different methods for rounding a number to two decimal places in C#: using Math.Floor, using Math.Ceiling, using Math.Round and using the String.Format functions.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.