Learn more about how rounding works in Datylon datasheets.
In Datylon, rounding is performed using the following formula:
Also written as:
Datylon Rounding = Math.round(number × (10 ^ digits)) / (10 ^ digits)
How Math.round Works
The rounding is based on JavaScript’s Math.round function, which behaves as follows:
- If the fractional portion of the number is greater than 0.5, the number is rounded to the next integer with the higher absolute value.
- If the fractional portion is less than 0.5, the number is rounded to the next integer with the lower absolute value.
- If the fractional portion is exactly 0.5, the number is rounded upward (toward +∞) regardless of whether it is positive or negative.
Example:
Rounding 10.55 with Precision 1
Let’s break down the steps for rounding 10.55 to precision 1 using Datylon’s rounding formula:
- Step 1: Multiply by 10^digits
Since the precision is 0, we calculate:
10.55 × (10 ^ 1) = 105.5 - Step 2: Apply Math.round
Next, apply the Math.round function to the result:
Math.round(105.5) = 106
This outcome occurs because the fractional portion (0.5) is exactly 0.5, the number is rounded upward (toward +∞) regardless of whether it is positive or negative. - Step 3: Divide by 10^digits
Finally, divide the result by 10^1:
106 / (10 ^ 1) = -10.6
Rounding -10.55 with Precision 1
- Step 1: Multiply by 10^digits
10.55 × (10 ^ 1) = -105.5 - Step 2: Apply Math.round
Math.round(-105.5) = 105
This outcome occurs because the fractional portion (0.5) is exactly 0.5, the number is rounded upward (toward +∞) regardless of whether it is positive or negative. - Step 3: Divide by 10^digits
Finally, divide the result by 10^1:
-105 / (10 ^ 1) = -10.5
Rounding -10.55 with Precision 0
- Step 1: Multiply by 10^digits
-10.55 × (10 ^ 0) = -10.55 - Step 2: Apply Math.round
Math.round(-10.55) = -11
This outcome occurs because the fractional portion (0.55) is greater than 0.5, so the number is rounded to the integer with the higher absolute value (-11). - Step 3: Divide by 10^digits
Finally, divide the result by 10^0:
-11 / (10 ^ 0) = -11