1

This little rounding issue got me puzzled. Admittedly I very rarely work with numbers and I wouldn't brag about my math skills, but I think 22.68 x 3 should be 68.04, but JavaScript insists on rounding to 68.03999999999999.

It does the same thing for multiplicative of 6 and 12, but not 9.

I wrote my sample converting a string because it's what I'm doing in my app, but the result doesn't change if we use numbers.

I couldn't find a clear explanation and I'm hoping I can get some insight on why this works the way it does so I can avoid making mistakes in the future.

Thank you.

    $(".canvas").on('click', ".button", function (e) {
  var numOne = Number('22.68');
  var numTwo = Number('3');
  var mathIshard = numOne * numTwo;
    $(".result").html(mathIshard);
    
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class='canvas'>

<div class='button'>
Click Me
</div>

<div class='result'>
result
</div>
</div>
Ionko Gueorguiev
  • 302
  • 4
  • 18

0 Answers0