Bug report: wrong calculation, addition with decimals

In a project I need to calculate a simple value. It is the addition of two numbers.

This works perfect in many cases but in some cases the additon is wrong.

If I have two input field with values 1.1 and 4.6 and calculate them 1.1+4.6 the result should be 5.7 but it is 5.6999999999.

I’v made a very simple prototype here:

with other values it works just fine.Error.rp (44.5 KB)

to me it seems to be a bug.

This isn’t a bug with Axure, it’s a result of JavaScript using floating point numbers. The only way to avoid it is to avoid using decimals in your calculation. You could instead do (11 + 46) / 10. If you expect to be working with more decimal places, multiply your decimal values by some larger multiple of 10 and then divide by that same amount at the end. If these numbers represent some monetary value, consider doing all calculations in cents and then converting to a larger denomination when you actually display the data.

I’ve had to do exactly that before when I prototyped a shopping cart. All prices were in cents (a $5.00 item was 500) and then wherever I showed it, I used $[[(value / 100).toFixed(2)]]

What causes floating point rounding errors?

1 Like