Which one rounds




















Rounds a double-precision floating-point value to an integer using the specified rounding convention. Rounds a double-precision floating-point value to a specified number of fractional digits, and rounds midpoint values to the nearest even number.

Rounds a decimal value to a specified number of fractional digits, and rounds midpoint values to the nearest even number. Rounds a double-precision floating-point value to the nearest integral value, and rounds midpoint values to the nearest even number. Rounds a decimal value to the nearest integral value, and rounds midpoint values to the nearest even number. In addition to the examples in the Remarks section, this article includes examples that illustrate the following overloads of the Math.

Round method:. Round Decimal Math. Round Double Math. Round Decimal, Int32 Math. Round Decimal, MidpointRounding Math. Round Double, Int32 Math. Round Double, MidpointRounding Math. Round Double, Int32, MidpointRounding. You can use the following table to select an appropriate rounding method. In addition to the Math. Round methods, it also includes Math. Ceiling and Math. Rounding involves converting a numeric value with a specified precision to a value with less precision.

For example, you can use the Round Double method to round a value of 3. In a midpoint value, the value after the least significant digit in the result is precisely half way between two numbers. For example, 3. In these cases, if the round-to-nearest strategy is used, the nearest value can't be easily identified without a rounding convention. The Round method supports two rounding conventions for handling midpoint values:.

Midpoint values are rounded to the next number away from zero. This form of rounding is represented by the MidpointRounding. AwayFromZero enumeration member. Midpoint values are rounded to the nearest even number. For example, both 3. ToEven enumeration member. NET Core 3.

These strategies are used in all cases, not just for midpoint values as MidpointRounding. ToEven and MidpointRounding. AwayFromZero are. Rounding away from zero is the most widely known form of rounding, while rounding to nearest even is the standard in financial and statistical operations. When used in multiple rounding operations, rounding to nearest even reduces the rounding error that is caused by consistently rounding midpoint values in a single direction.

In some cases, this rounding error can be significant. The following example illustrates the bias that can result from consistently rounding midpoint values in a single direction.

The example computes the true mean of an array of Decimal values, and then computes the mean when the values in the array are rounded by using the two conventions. In this example, the true mean and the mean that results when rounding to nearest are the same. However, the mean that results when rounding away from zero differs by. By default, the Round method uses the round to nearest even convention.

The following table lists the overloads of the Round method and the rounding convention that each uses. In order to determine whether a rounding operation involves a midpoint value, the Round method multiplies the original value to be rounded by 10 n , where n is the desired number of fractional digits in the return value, and then determines whether the remaining fractional portion of the value is greater than or equal to.

This is a slight variation on a test for equality, and as discussed in the "Testing for Equality" section of the Double reference topic, tests for equality with floating-point values are problematic because of the floating-point format's issues with binary representation and precision.

This means that any fractional portion of a number that is slightly less than. The following example illustrates the problem. It repeatedly adds. However, as the output from the example shows, it does not. The example uses the "R" standard numeric format string to display the floating point value's full precision, and shows that the value to be rounded has lost precision during repeated additions, and its value is actually As the example also shows, this problem does not occur if you assign the constant value Problems of precision in rounding midpoint values are most likely to arise in the following conditions:.

Yes No. Thank you! Any more feedback? The more you tell us the more we can help. Can you help us improve? Resolved my issue. Clear instructions. Easy to follow. No jargon. Pictures helped. Didn't match my screen. Incorrect instructions.

Use one of the following constants to specify the mode in which rounding occurs. The value rounded to the given precision as a float. Example 1 round examples. Example 2 How precision affects a float. Example 3 mode examples. Example 4 mode with precision examples. Submit a Pull Request Report a Bug. Parameters num The value to round. Return Values The value rounded to the given precision as a float.

Changelog Version Description 8. Rounding modes with 9. In accounting, it's often necessary to always round up, or down to a precision of thousandths. As PHP doesn't have a a native number truncate function, this is my solution - a function that can be usefull if you need truncate instead round a number. When you have a deal with money like dollars, you need to display it under this condition: -format all number with two digit decimal for cents.

I discovered that under some conditions you can get rounding errors with round when converting the number to a string afterwards.



0コメント

  • 1000 / 1000