Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Decimal.Round() Method in C#
The Decimal.Round() method in C# is used to round a decimal value to the nearest integer or to a specified number of decimal places. This method provides several overloads to handle different rounding scenarios and precision requirements.
Syntax
The Decimal.Round() method has four main overloads −
public static decimal Round(decimal d); public static decimal Round(decimal d, int decimals); public static decimal Round(decimal d, MidpointRounding mode); public static decimal Round(decimal d, int decimals, MidpointRounding mode);
Parameters
-
d − The decimal number to be rounded.
-
decimals − The number of decimal places in the return value (0 to 28).
-
mode − Specification for how to round if the value is midway between two others.
Return Value
Returns a decimal number rounded to the nearest integer or to the specified number of decimal places.
Using Basic Decimal Rounding
Example
using System;
public class Demo {
public static void Main() {
decimal val1 = 9.00m;
decimal val2 = 15.29m;
decimal val3 = 394949845.14245m;
Console.WriteLine("Original values:");
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("\nRounded to nearest integer:");
Console.WriteLine("Value 1 (Rounded) = " + Decimal.Round(val1));
Console.WriteLine("Value 2 (Rounded) = " + Decimal.Round(val2));
Console.WriteLine("Value 3 (Rounded) = " + Decimal.Round(val3));
}
}
The output of the above code is −
Original values: Decimal 1 = 9.00 Decimal 2 = 15.29 Decimal 3 = 394949845.14245 \nRounded to nearest integer: Value 1 (Rounded) = 9 Value 2 (Rounded) = 15 Value 3 (Rounded) = 394949845
Using Decimal Rounding with Precision
Example
using System;
public class Demo {
public static void Main() {
decimal val1 = 6.59m;
decimal val2 = 30.127m;
decimal val3 = 6946649845.25245m;
Console.WriteLine("Original values:");
Console.WriteLine("Decimal 1 = " + val1);
Console.WriteLine("Decimal 2 = " + val2);
Console.WriteLine("Decimal 3 = " + val3);
Console.WriteLine("\nRounded with specified decimal places:");
Console.WriteLine("Value 1 (1 decimal) = " + Decimal.Round(val1, 1));
Console.WriteLine("Value 2 (1 decimal) = " + Decimal.Round(val2, 1));
Console.WriteLine("Value 3 (2 decimals) = " + Decimal.Round(val3, 2));
}
}
The output of the above code is −
Original values: Decimal 1 = 6.59 Decimal 2 = 30.127 Decimal 3 = 6946649845.25245 Rounded with specified decimal places: Value 1 (1 decimal) = 6.6 Value 2 (1 decimal) = 30.1 Value 3 (2 decimals) = 6946649845.25
Using MidpointRounding Options
The MidpointRounding enumeration specifies how to round when a value is exactly halfway between two numbers −
Example
using System;
public class Demo {
public static void Main() {
decimal value = 2.5m;
Console.WriteLine("Original value: " + value);
Console.WriteLine("ToEven (default): " + Decimal.Round(value, MidpointRounding.ToEven));
Console.WriteLine("AwayFromZero: " + Decimal.Round(value, MidpointRounding.AwayFromZero));
decimal negValue = -2.5m;
Console.WriteLine("\nOriginal negative value: " + negValue);
Console.WriteLine("ToEven: " + Decimal.Round(negValue, MidpointRounding.ToEven));
Console.WriteLine("AwayFromZero: " + Decimal.Round(negValue, MidpointRounding.AwayFromZero));
}
}
The output of the above code is −
Original value: 2.5 ToEven (default): 2 AwayFromZero: 3 Original negative value: -2.5 ToEven: -2 AwayFromZero: -3
Comparison of Rounding Methods
| Method | Description | Example (2.5) |
|---|---|---|
| ToEven (Banker's Rounding) | Rounds to the nearest even number | 2 |
| AwayFromZero | Rounds away from zero | 3 |
Conclusion
The Decimal.Round() method provides flexible decimal rounding with options for precision control and midpoint handling. Use MidpointRounding.ToEven for financial calculations to reduce bias, and MidpointRounding.AwayFromZero for standard mathematical rounding.
