Rounding Modes

Last updated on Nov 19, 2021

Floor

Rounds the number towards negative infinity, It always outputs the nearest integer value which is equal to or lesser than the given number.


Ceiling

Rounds the number towards positive infinity, It always outputs the nearest integer value which is equal to or greater than the given number.


Round Up

Rounding mode to round away from zero. Always increments the digit prior to a non-zero discarded fraction. This rounding mode never decreases the magnitude of the calculated value.

For example,

Input Value Scale Result
10.105 1 10.2
10.105 2 10.11
10.100 1 10.1
10.100 -1 20
10.100 -2 1
10.100 -3 100

Round Down

Rounding mode to round towards zero. This never increments the digit prior to a discarded fraction.

Note: This rounding mode never increases the magnitude of the calculated value.


Round Half Up

Behaves as for Round Up if the discarded fraction is >= 0.5; otherwise, behaves as for Round Down.


Round Half Down

Behaves as for Round Up if the discarded fraction is > 0.5; otherwise, behaves as for Round Down.

For example,

Input Value Scale Result
10.105 1 10.1
10.105 2 10.10
10.100 -1 10

Calculation of Discarded Fraction Digits

  • On decreasing scale, discarded fraction is the fraction part after the last digit according to the new scale.

  • If the scale is zero or negative, the entire fraction part is treated as discarded.

Changes in scale

  • If the scale is increased, insignificant zeroes are added to the end of fraction so that the number of fraction digits are same as the scale.

  • If the scale is decreased and is positive, digits at the end of the fraction are discarded so that the number of fraction digits are same as the scale.

  • If the scale is set to negative, fraction point is shifted towards left by the magnitude of scale and the number is multiplied by 10 power of magnitude of scale to maintain its overall value.

Considerations

  • No changes are made if the input value is an integer or a decimal with only zeroes.

  • On increasing the scale, there is no effect on the output due to rounding.

  • On decreasing the scale, the output value may increase or decrease depending on the choice of rounding mode.

  • If the new scale is same as the previous scale, there is no effect on output due to rounding.

Tell us what went wrong