Weird SUM calculation results

Hello Wapplers,

I have pretty basic repeat where I'm calculating work hours and then after repeat I'm calculating SUM of those works.

repeat_calculation.sum('calculation_man_hour')

But I see that calculation results are weird:

"repeat_calculation": [
{
"job_id": 1,
"calculation_man_hour": null,
},
{
"job_id": 2,
"calculation_man_hour": "28.81",
},
{
"job_id": 3,
"calculation_man_hour": "95.79",
}
],
"total_man_hour": 124.60000000000001,

I've highlighted number that I'm calculating with SUM and exact SUM with bold.
Is there something I should additional tweak the SUM? This happens because of NULL result in one of the entries?

That’s how javascript calculations work. You can test it in your browser console as well to check the results:

Screenshot 2021-10-12 at 10.59.12

there are many articles in the internet explaining this, for example:

https://medium.com/@DominicCarmel/understanding-javascripts-weird-decimal-calculations-e65f0e1adefb

You can just apply some formatting to your result so you always get the same number of decimals.

2 Likes

@Teodor Thanks for a quick and precise answer.

Another option is multiplying by 100, to turn the decimal numbers into integers, and then dividing by 100.

image

1 Like

Nah, formatNumber() did what exactly need to be! :slight_smile:

I was only just suggesting another option. However, I think converting decimals to integers is often a better solution - eg for calculations relating to currency.