MathF.Round Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
Round(Single, Int32, MidpointRounding) |
Rounds a single-precision floating-point value to a specified number of fractional digits using the specified rounding convention. |
Round(Single, MidpointRounding) |
Rounds a single-precision floating-point value to an integer using the specified rounding convention. |
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) |
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, Int32, MidpointRounding)
- Source:
- MathF.cs
- Source:
- MathF.cs
- Source:
- MathF.cs
Rounds a single-precision floating-point value to a specified number of fractional digits using the specified rounding convention.
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
Parameters
- x
- Single
A single-precision floating-point number to be rounded.
- digits
- Int32
The number of fractional digits in the return value.
- mode
- MidpointRounding
One of the enumeration values that specifies which rounding strategy to use.
Returns
The number that x
is rounded to that has digits
fractional digits. If x
has fewer fractional digits than digits
, x
is returned unchanged.
Exceptions
digits
is less than 0 or greater than 6.
mode
is not a valid value of MidpointRounding.
Remarks
The value of the digits
argument can range from 0 to 6. The maximum number of integral and fractional digits supported by the Single type is 6.
Important
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.
If the value of the x
argument is Single.NaN, the method returns Single.NaN. If x
is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.
See also
Applies to
Round(Single, MidpointRounding)
- Source:
- MathF.cs
- Source:
- MathF.cs
- Source:
- MathF.cs
Rounds a single-precision floating-point value to an integer using the specified rounding convention.
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
Parameters
- x
- Single
A single-precision floating-point number to be rounded.
- mode
- MidpointRounding
One of the enumeration values that specifies which rounding strategy to use.
Returns
The integer that x
is rounded to using the mode
rounding convention. This method returns a Single instead of an integral type.
Exceptions
mode
is not a valid value of MidpointRounding.
Remarks
Important
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.
If the value of the x
argument is Single.NaN, the method returns Single.NaN. If x
is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.
See also
Applies to
Round(Single)
- Source:
- MathF.cs
- Source:
- MathF.cs
- Source:
- MathF.cs
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
Parameters
- x
- Single
A single-precision floating-point number to be rounded.
Returns
The integer nearest 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. Note that this method returns a Single instead of an integral type.
Remarks
This method uses the default rounding convention of MidpointRounding.ToEven.
Important
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.
If the value of the x
argument is Single.NaN, the method returns Single.NaN. If x
is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.
Notes to Callers
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.
See also
Applies to
Round(Single, Int32)
- Source:
- MathF.cs
- Source:
- MathF.cs
- Source:
- MathF.cs
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
Parameters
- x
- Single
A single-precision floating-point number to be rounded.
- digits
- Int32
The number of fractional digits in the return value.
Returns
The number nearest to x
that contains a number of fractional digits equal to digits
.
Exceptions
digits
is less than 0 or greater than 6.
Remarks
The value of the digits
argument can range from 0 to 6. Note that 6 is the maximum number of integral and fractional digits supported by the Single type.
This method uses the default rounding convention of MidpointRounding.ToEven.
Important
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.
If the value of the x
argument is Single.NaN, the method returns Single.NaN. If x
is Single.PositiveInfinity or Single.NegativeInfinity, the method returns Single.PositiveInfinity or Single.NegativeInfinity, respectively.
See also
Applies to
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for