Sum of values with reduce

Hi,

sorry, i really confused with javascript sometime. Can anyone explain the Result of:

const data = datatable.reduce((acc, bill) => bill + acc);
return data;

the values of datatable are:
[
37.72,
63.47,
1985.13,
2667.21,
-2667.21,
606.47,
561.03
]

and The Javascript Result is: 3253.8200000000006
Normaly the result should: 3253,82

I don´t understand these 00000000006. Does anyone know this? That would be really great.

thx
Franz

Hi Franz! This happens because, in Javascript, all numbers are encoded as double precision floating point numbers, following the international IEEE 754 standard. You can also check this W3Schools resource for additional information.

Hi Amelia,

wow very unexpected… :rofl: Never faced with such a problem in many different languages.
If I have understood correctly, the values must be multiplied by 10. Do you know is there also a workaround that works together with ‘reduce’?

I did a
return data.toFixed(2)
for now. It works, but it is a quick an dirty solution :innocent:

thx
Franz