please wait

  • Dipti Singh Mar-11-2019 07:37:58 AM ( 2 months ago )

    I am having an issue with the way Javascript is rounding numbers when hitting 0.5. I am writing levies calculators, and am noticing a 0.1c discrepancy in the results.

    The problem is that the result for them is 21480.705 which my application translates into 21480.71, whereas the tariff says 21480.70.

    This is what I am seeing with Javascript:



    • What the hell is going on with this erratic rouding?
    • What's the quickest easiest way to get a "rounded up" result (when hitting 0.5)?
  • Ekta Singhania Mar-11-2019 07:39:21 AM ( 2 months ago )

    This seems to do the trick.

    The rounding problem can be avoided by using numbers represented in exponential notation:

    function round(value, decimals) {
      return Number(Math.round(value+'e'+decimals)+'e-'+decimals);
    console.log(round(21480.105, 2).toFixed(2));

Please login

Similar Discussion

Recommended For You