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
MathF.Round() Method in C# with Examples
The MathF.Round() method in C# is used to round a float value to the nearest integer or to a specified number of fractional digits. This method provides several overloads to handle different rounding scenarios and midpoint rounding behaviors.
Syntax
Following are the different overloads of the MathF.Round() method −
public static float Round(float x); public static float Round(float x, int digits); public static float Round(float x, int digits, MidpointRounding mode); public static float Round(float x, MidpointRounding mode);
Parameters
x − The
floatnumber to be rounded.digits − The number of fractional digits in the return value.
mode − Specifies how to round
xif it is midway between two numbers using theMidpointRoundingenumeration.
Return Value
Returns a float number nearest to x that contains a number of fractional digits equal to digits.
Using MathF.Round() with Basic Rounding
Example
using System;
public class Demo {
public static void Main() {
float val1 = 15.20f;
float val2 = 3.10f;
float val3 = 7.75f;
float val4 = 9.25f;
Console.WriteLine("Original values:");
Console.WriteLine("val1 = " + val1);
Console.WriteLine("val2 = " + val2);
Console.WriteLine("val3 = " + val3);
Console.WriteLine("val4 = " + val4);
Console.WriteLine("\nRounded values:");
Console.WriteLine("Rounded (val1) = " + MathF.Round(val1));
Console.WriteLine("Rounded (val2) = " + MathF.Round(val2));
Console.WriteLine("Rounded (val3) = " + MathF.Round(val3));
Console.WriteLine("Rounded (val4) = " + MathF.Round(val4));
}
}
The output of the above code is −
Original values: val1 = 15.2 val2 = 3.1 val3 = 7.75 val4 = 9.25 Rounded values: Rounded (val1) = 15 Rounded (val2) = 3 Rounded (val3) = 8 Rounded (val4) = 9
Using MathF.Round() with Specified Decimal Places
Example
using System;
public class Demo {
public static void Main() {
float val1 = 10.23898f;
float val2 = 20.878788f;
float val3 = 15.12345f;
Console.WriteLine("Original values:");
Console.WriteLine("val1 = " + val1);
Console.WriteLine("val2 = " + val2);
Console.WriteLine("val3 = " + val3);
Console.WriteLine("\nRounded to different decimal places:");
Console.WriteLine("Rounded (val1, 2) = " + MathF.Round(val1, 2));
Console.WriteLine("Rounded (val2, 3) = " + MathF.Round(val2, 3));
Console.WriteLine("Rounded (val3, 1) = " + MathF.Round(val3, 1));
Console.WriteLine("Rounded (val3, 4) = " + MathF.Round(val3, 4));
}
}
The output of the above code is −
Original values: val1 = 10.23898 val2 = 20.878788 val3 = 15.12345 Rounded to different decimal places: Rounded (val1, 2) = 10.24 Rounded (val2, 3) = 20.879 Rounded (val3, 1) = 15.1 Rounded (val3, 4) = 15.1235
Using MathF.Round() with MidpointRounding
Example
using System;
public class Demo {
public static void Main() {
float val = 2.5f;
Console.WriteLine("Original value: " + val);
Console.WriteLine("ToEven (Banker's): " + MathF.Round(val, MidpointRounding.ToEven));
Console.WriteLine("AwayFromZero: " + MathF.Round(val, MidpointRounding.AwayFromZero));
val = 3.5f;
Console.WriteLine("\nOriginal value: " + val);
Console.WriteLine("ToEven (Banker's): " + MathF.Round(val, MidpointRounding.ToEven));
Console.WriteLine("AwayFromZero: " + MathF.Round(val, MidpointRounding.AwayFromZero));
val = -2.5f;
Console.WriteLine("\nOriginal value: " + val);
Console.WriteLine("ToEven (Banker's): " + MathF.Round(val, MidpointRounding.ToEven));
Console.WriteLine("AwayFromZero: " + MathF.Round(val, MidpointRounding.AwayFromZero));
}
}
The output of the above code is −
Original value: 2.5 ToEven (Banker's): 2 AwayFromZero: 3 Original value: 3.5 ToEven (Banker's): 4 AwayFromZero: 4 Original value: -2.5 ToEven (Banker's): -2 AwayFromZero: -3
MidpointRounding Modes Comparison
| Mode | Description | Example: 2.5 rounds to |
|---|---|---|
| ToEven | Rounds to the nearest even number (Banker's rounding) | 2 |
| AwayFromZero | Rounds away from zero to the nearest number | 3 |
Conclusion
The MathF.Round() method provides flexible rounding capabilities for float values in C#. It supports rounding to the nearest integer, to a specified number of decimal places, and offers different midpoint rounding strategies to handle edge cases where values are exactly halfway between two numbers.
