Search code examples
javascriptshopping-cartcalculated-columns

calculating javascript (simplecart) cart total, adds random decimals to total


i have a unique problem and am about to loose my sanity.

im using a js cart (simplecart), and when i add something to the cart it adds a 2 or subtracts a 3 from the price, in the 15th decimal:

this is what the products costs: 35,99 This is what it calculates as total when i add this single product:

€ 35,990000000000002

i thought it might have to do with shipping or taxes, but even after disabling those modules, its still the same. This price is also displayed as the item price in the cart.

here my jsfiddle:

http://jsfiddle.net/dJYNk/

how should i go about resolving this? I have no idea when this sneaked in, allready stripped the whole project trying to isolate the problem, but it occurs even in this minimal setup, and i seem to remember this cart working pretty well.

What might be the problem?


Solution

  • simpleCart.currency({
        code: "EUR" , 
          name: "Euro" ,
        symbol: " € " ,
        delimiter: " " , 
        decimal: "," , 
        after: false ,
        accuracy: 2
    });
    

    Change the accuracy to two places. You should use server-side validation to calculate the final cost and only use javascript to indicate the cost to the user.