When I multiply 100*4.6
or 100*4.9
, the code produces 459.99999999999994
and 490.00000000000006
, respectively. It only does this for these specific numbers. When I multiply 100*4.7
, for example, it correctly produces the number 470
.
I tried parseFloat()
on a whim and it did not help (nor did I expect it to). I do not want to use Math.round
or .toFixed()
because, in my full code, I randomly generate the number that gets multiplied by 100, so sometimes it should have a decimal. (For example, if the code generates 100 * 3.652
, the answer should be 365.2
, so I do not want to simply round as a band-aid solution.)
Why is this multiplication error happening for these specific numbers?
Javascript:
var Product = 100* 4.6;
$(".Product").html(Product);
var Product2 = 100* 4.7;
$(".Product2").html(Product2);
var Product3 = 100* 4.9;
$(".Product3").html(Product3);
HTML:
<span class="Product"></span>
<br>
<span class="Product2"></span>
<br>
<span class="Product3"></span>
JSFiddle: