MathF.Round 方法

定义

重载

Round(Single, Int32, MidpointRounding)

将单精度浮点值舍入到指定的小数位数,并为中点值使用指定的舍入规则。Rounds a single-precision floating-point value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

Round(Single, MidpointRounding)

将单精度浮点值舍入到最接近的整数,并为中点值使用指定的舍入规则。Rounds a single-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values.

Round(Single, Int32)

将单精度浮点值舍入到指定的小数位数,并将中点值舍入到最接近的偶数。Rounds a single-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

Round(Single)

将单精度浮点值舍入到最接近的整数值,并将中点值舍入到最接近的偶数。Rounds a single-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.

Round(Single, Int32, MidpointRounding)

将单精度浮点值舍入到指定的小数位数,并为中点值使用指定的舍入规则。Rounds a single-precision floating-point value to a specified number of fractional digits, and uses the specified rounding convention for midpoint values.

public:
 static float Round(float x, int digits, MidpointRounding mode);
public static float Round (float x, int digits, MidpointRounding mode);
static member Round : single * int * MidpointRounding -> single
Public Shared Function Round (x As Single, digits As Integer, mode As MidpointRounding) As Single

参数

x
Single

要舍入的单精度浮点数。A single-precision floating-point number to be rounded.

digits
Int32

返回值中的小数数字。The number of fractional digits in the return value.

mode
MidpointRounding

在两个数字之间时如何舍入 x 的规范。Specification for how to round x if it is midway between two other numbers.

返回

最接近 xdigits 位小数的数字。The number nearest to x that has a number of fractional digits equal to digits. 如果 xdigits 少部分数字,x 原样返回。If x has fewer fractional digits than digits, x is returned unchanged.

异常

digits 小于 0 或大于 6。digits is less than 0 or greater than 6.

mode 不是 MidpointRounding 的一个有效值。mode is not a valid value of MidpointRounding.

注解

digits参数的值的范围可以介于0到6之间。The value of the digits argument can range from 0 to 6. 请注意, 6 是该Single类型支持的整数和小数位数的最大值。Note that 6 is the maximum number of integral and fractional digits supported by the Single type.

重要

在舍入中点值时,舍入算法执行相等测试。When rounding midpoint values, the rounding algorithm performs an equality test. 由于浮点格式存在的二进制表示和精度问题,因此方法返回的值可能异常。Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. 有关详细信息,请参阅“舍入和精度”For more information, see "Rounding and precision".

如果x参数的值为Single.NaN, 则该方法返回Single.NaNIf the value of the x argument is Single.NaN, the method returns Single.NaN. 如果xSingle.PositiveInfinitySingle.PositiveInfinity Single.NegativeInfinity, 则该方法将分别返回或。 Single.NegativeInfinityIf x is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.

另请参阅

Round(Single, MidpointRounding)

将单精度浮点值舍入到最接近的整数,并为中点值使用指定的舍入规则。Rounds a single-precision floating-point value to the nearest integer, and uses the specified rounding convention for midpoint values.

public:
 static float Round(float x, MidpointRounding mode);
public static float Round (float x, MidpointRounding mode);
static member Round : single * MidpointRounding -> single
Public Shared Function Round (x As Single, mode As MidpointRounding) As Single

参数

x
Single

要舍入的单精度浮点数。A single-precision floating-point number to be rounded.

mode
MidpointRounding

在两个数字之间时如何舍入 x 的规范。Specification for how to round x if it is midway between two other numbers.

返回

最接近 x 的整数。The integer nearest x. 如果 x 是两个整数的中值,这两个整数一个为偶数,另一个为奇数,则 mode 确定返回两个整数中的哪一个。If x is halfway between two integers, one of which is even and the other odd, then mode determines which of the two is returned. 请注意,此方法返回 Single,而不是整数类型。Note that this method returns a Single instead of an integral type.

异常

mode 不是 MidpointRounding 的一个有效值。mode is not a valid value of MidpointRounding.

注解

重要

在舍入中点值时,舍入算法执行相等测试。When rounding midpoint values, the rounding algorithm performs an equality test. 由于浮点格式存在的二进制表示和精度问题,因此方法返回的值可能异常。Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. 有关详细信息,请参阅“舍入和精度”For more information, see "Rounding and precision".

如果x参数的值为Single.NaN, 则该方法返回Single.NaNIf the value of the x argument is Single.NaN, the method returns Single.NaN. 如果xSingle.PositiveInfinitySingle.PositiveInfinity Single.NegativeInfinity, 则该方法将分别返回或。 Single.NegativeInfinityIf x is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.

另请参阅

Round(Single, Int32)

将单精度浮点值舍入到指定的小数位数,并将中点值舍入到最接近的偶数。Rounds a single-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

public:
 static float Round(float x, int digits);
public static float Round (float x, int digits);
static member Round : single * int -> single
Public Shared Function Round (x As Single, digits As Integer) As Single

参数

x
Single

要舍入的单精度浮点数。A single-precision floating-point number to be rounded.

digits
Int32

返回值中的小数数字。The number of fractional digits in the return value.

返回

最接近 xdigits 位小数的数字。The number nearest to x that contains a number of fractional digits equal to digits.

异常

digits 小于 0 或大于 6。digits is less than 0 or greater than 6.

注解

digits参数的值的范围可以介于0到6之间。The value of the digits argument can range from 0 to 6. 请注意, 6 是该Single类型支持的整数和小数位数的最大值。Note that 6 is the maximum number of integral and fractional digits supported by the Single type.

此方法使用的默认舍入约定MidpointRounding.ToEvenThis method uses the default rounding convention of MidpointRounding.ToEven.

重要

在舍入中点值时,舍入算法执行相等测试。When rounding midpoint values, the rounding algorithm performs an equality test. 由于浮点格式存在的二进制表示和精度问题,因此方法返回的值可能异常。Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. 有关详细信息,请参阅“舍入和精度”For more information, see "Rounding and precision".

如果x参数的值为Single.NaN, 则该方法返回Single.NaNIf the value of the x argument is Single.NaN, the method returns Single.NaN. 如果xSingle.PositiveInfinitySingle.PositiveInfinity Single.NegativeInfinity, 则该方法将分别返回或。 Single.NegativeInfinityIf x is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.

另请参阅

Round(Single)

将单精度浮点值舍入到最接近的整数值,并将中点值舍入到最接近的偶数。Rounds a single-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number.

public:
 static float Round(float x);
public static float Round (float x);
static member Round : single -> single
Public Shared Function Round (x As Single) As Single

参数

x
Single

要舍入的单精度浮点数。A single-precision floating-point number to be rounded.

返回

最接近 x 的整数。The integer nearest x. 如果 x 的小数部分正好处于两个整数中间,其中一个整数为偶数,另一个整数为奇数,则返回偶数。If the fractional component of x is halfway between two integers, one of which is even and the other odd, then the even number is returned. 请注意,此方法返回 Single,而不是整数类型。Note that this method returns a Single instead of an integral type.

注解

此方法使用的默认舍入约定MidpointRounding.ToEvenThis method uses the default rounding convention of MidpointRounding.ToEven.

重要

在舍入中点值时,舍入算法执行相等测试。When rounding midpoint values, the rounding algorithm performs an equality test. 由于浮点格式存在的二进制表示和精度问题,因此方法返回的值可能异常。Because of problems of binary representation and precision in the floating-point format, the value returned by the method can be unexpected. 有关详细信息,请参阅“舍入和精度”For more information, see "Rounding and precision".

如果x参数的值为Single.NaN, 则该方法返回Single.NaNIf the value of the x argument is Single.NaN, the method returns Single.NaN. 如果xSingle.PositiveInfinitySingle.PositiveInfinity Single.NegativeInfinity, 则该方法将分别返回或。 Single.NegativeInfinityIf x is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.

调用方说明

由于将十进制值表示为浮点数或对浮点值执行算术运算可能导致精度损失, 因此, 在某些情况下, 该Round(Single)方法可能不会显示为舍入点值最接近的整数。Because of the loss of precision that can result from representing decimal values as floating-point numbers or performing arithmetic operations on floating-point values, in some cases the Round(Single) method may not appear to round midpoint values to the nearest even integer.

另请参阅

适用于