Rounding decimal following standard ABNT 5891 [closed]

1

Good morning!

I have to implement a rounding rule in JavaScript following the ABNT NBR 5891: 2014 rule. . I tried to use only the existing functions like Math.round, Math.trunc, Math.floor, Math.ceil, but none gave 100%, the closest was Math.round.

The rule is as follows:

  • When the digit next to the last digit to be stored is less than 5, the last digit to be stored remains unchanged.
  • When the figure immediately following the last digit to be stored is greater than 5, or, if 5 is followed by at least one digit other than zero, the last digit to be retained shall be increased by one. li>
  • When the digit next to the last digit to be stored is followed by zeros, the digit to be retained should be rounded to the nearest digit. Consequently, the last number to be withdrawn, if odd, will increase by one.
  • When the digit next to the last one to be retained is followed by zeros, if there is even the digit to be retained, it will remain unchanged.
  • In the examples below we are applying the ABNT rule rounding up to 2 decimal places. Notice that 0 is considered even.

    EX: [0.342, 0.346, 0.3452, 0.3450, 0.332, 0.336, 0.3352, 0.3350, 0.3050, 0.3150]

    By the rule: [0.34, 0.35, 0.35, 0.34, 0.33, 0.34, 0.34, 0.34, 0.3, 0.32] , 0.35 , 0.35, 0.33 , 0.34 / strong>, 0.34 , 0.34 , 0.31, 0.32 , 0.33 , 0.33, 0.33, 0.33, 0.3 strong>, 0.31]

    0.33, 0.33, 0.33, 0.3 , 0.33 , 0.33 strong>, 0.31]

    , 0.35 , 0.35 , 0.35, 0.34, 0.34 0.34 , 0.31, 0.32

    What would be the best way to adapt these rules in JavaScript?

        
    asked by anonymous 29.11.2018 / 13:08

    0 answers