Math.Round 方法 (Double, Int32)


返回具有指定精度、最接近指定值的数。

[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;
参数
value
要舍入的双精度浮点数。
digits
返回值中的有效小数位数(精度)。
返回值
精度等于 digits 的、最接近 value 的数字。如果 value 位于两个数字的中间,其中一个为偶数,另一个为奇数,则返回偶数。如果 value 的精度小于 digits,则返回 value 而不做更改。

备注
digits 参数指定返回值的有效小数位数,其范围在 0 到 15 之间。如果 digits 为零,则返回一个整数。

此方法的行为遵循 IEEE 标准 754 的第 4 节。这种舍入有时称为就近舍入或银行家舍入。如果 digits 为零,则这种舍入有时称为“向零舍入”。

示例
[Visual Basic, C#, C++] 下面的代码演示就近舍入。

[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.

本文来自: 乘风原创程序(http://www.qqcf.com) 详细出处参考:http://study.qqcf.com/web/718/239806.htm