The Math.Round() method in C# rounds a value to the nearest integer or to the specified number of fractional digits.
Methods
The following are the methods overloaded by Math.Round()
1 2 3 4 5 6 7 8 9 10 |
Math.Round(Double) Math.Round(Double, Int32) Math.Round(Double, Int32, MidpointRounding) Math.Round(Double, MidpointRounding) Math.Round(Decimal) Math.Round(Decimal, Int32) Math.Round(Decimal, Int32, MidpointRounding) Math.Round(Decimal, MidpointRounding) |
Example
Let us now see an example to implement Math.Round() method i.e. Math.Round(Decimal)
1 2 3 4 5 6 7 8 9 10 11 12 |
static void Main(string[] args) { Decimal val1 = 42.52m; Decimal val2 = 10.3455m; Console.WriteLine("Decimal Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Decimal Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); Console.ReadKey(); } |
Output:
Example
Let us see another example to implement Math.Round() method i.e. Math.Round(Double)
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 |
static void Main(string[] args) { Double val1 = 23.10; Double val2 = 90.98; Console.WriteLine("Double Value = " + val1); Console.WriteLine("Rounded value = " + Math.Round(val1)); Console.WriteLine("Double Value = " + val2); Console.WriteLine("Rounded value = " + Math.Round(val2)); Console.ReadKey(); } |
Output: