Mono Class Library: System.Math Overview | Members

System.Math.Round Method

Returns the number nearest the specified double within the specified precision.

public static double Round (double value, int digits)

Parameters

value
A double to be rounded.
digits
A int containing the value of the number of significant fractional digits (precision) in the return value. This number is required to be greater than or equal to 0 and less than or equal to 15.
digits
Documentation for this section has not yet been entered.

Returns

A double containing the value of the number nearest value with a precision equal to digits . If the digit in value that is in the 10-(digits + 1) place is equal to 5 and there are no non-zero numbers in any less significant place, then the digit in the 10-digits place will be unchanged if it is even, else it will be set to the closest even integer value in the direction of the digit in the 10-(digits + 1) place. If the precision of value is less than digits, then value is returned unchanged. If digits is zero, this method behaves in the same manner as Math.Round(double) (value ).

Exceptions

TypeReason
ArgumentOutOfRangeException

digits < 0

-or-

digits > 15

Remarks

The behavior of this method follows IEEE Standard 754, section 4.1.

Example

The following example demonstrates using the Math.Round(double)(double, int) method.

C# Example

using System;

public class MathRoundExample
{

   public static void Main()
   {

      Double d1 = Math.Round(3.44,1);
      Double d2 = Math.Round(3.45,1);
      Double d3 = Math.Round(3.55,1);
      Console.WriteLine("Math.Round(3.44, 1) returns {0}", d1);
      Console.WriteLine("Math.Round(3.45, 1) returns {0}", d2);
      Console.WriteLine("Math.Round(3.55, 1) returns {0}", d3);

   }

}

The output is

Math.Round(3.44, 1) returns 3.4
Math.Round(3.45, 1) returns 3.4
Math.Round(3.55, 1) returns 3.6

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0