So I am trying to load an ecommerce checkout but based on the number a user enters:
var finalAmount = convert_amount( $('#input').val() );
The convert amount is to return the amount * 100 like this:
function convert_amount( amount ) {
return ( amount * 100 );
}
Ok so here's the problem. For most numbers it works ok, but for some that I have tried it doesn't work correctly. If I enter something like 8.32
then it returns 832
which is perfect. I found that if I enter 8.72
then it returns 872.0000000000001
. I don't get why it is adding the .0000000000001
onto the number.
Another interesting case is if I put in 8.70
it spits back 869.9999999999999
.
It was recommended to me to try doing return ( new Number( amount ) * 100 );
but that still did not do the trick.
The reason this is a major problem is because it makes the ecommerce platform reject the amount, saying it is invalid.
Any idea or experiences with this are greatly appreciated.
Thanks!