Math.Round Method (Double, Int32)
Returns the number with the specified precision nearest the specified value.
[Visual Basic] Overloads Public Shared Function Round( _ ByVal value As Double, _ ByVal digits As Integer _ ) As Double [C#] public static double Round( double value, int digits ); [C++] public: static double Round( double value, int digits ); [JScript] public static function Round( value : double, digits : int ) : double;
Parameters
- value
- A double-precision floating-point number to be rounded.
- digits
- The number of significant digits (precision) in the return value.
Return Value
The number nearest value with precision equal to digits. If value is halfway between two numbers, one of which is even and the other odd, then the even number is returned. If the precision of value is less than digits, then value is returned unchanged.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentOutOfRangeException | digits is less than 0 or greater than 15. |
Remarks
The digits parameter specifies the number of significant digits in the return value and ranges from 0 to 15. If digits is zero, then a whole number is returned.
The maximum total number of integral and fractional digits that can be returned is 15. If the rounded value contains more than 15 integral and fractional digits, the 15 most significant digits are returned. If the rounded value contains 15 or fewer integral and fractional digits, the integral digits and as many fractional digits as the digits parameter specifies are returned.
The behavior of this method follows IEEE Standard 754, section 4. This kind of rounding is sometimes called rounding to nearest, or banker's rounding. If digits is zero, this kind of rounding is sometimes called rounding toward zero.
Example
The following code example demonstrates rounding to nearest.
[Visual Basic] Math.Round(3.44, 1) 'Returns 3.4. Math.Round(3.45, 1) 'Returns 3.4. Math.Round(3.46, 1) 'Returns 3.5. [C#] Math.Round(3.44, 1); //Returns 3.4. Math.Round(3.45, 1); //Returns 3.4. Math.Round(3.46, 1); //Returns 3.5. [C++] Math::Round(3.44, 1); //Returns 3.4. Math::Round(3.45, 1); //Returns 3.4. Math::Round(3.46, 1); //Returns 3.5. [JScript] System.Math.Round(3.44, 1) //Returns 3.4. System.Math.Round(3.45, 1) //Returns 3.4. System.Math.Round(3.46, 1) //Returns 3.5.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
Math Class | Math Members | System Namespace | Math.Round Overload List | Ceiling | Floor