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?
Teodor
October 12, 2021, 8:01am
2
That’s how javascript calculations work. You can test it in your browser console as well to check the results:
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.
TomD
October 12, 2021, 12:08pm
4
Another option is multiplying by 100, to turn the decimal numbers into integers, and then dividing by 100.
1 Like
Nah, formatNumber() did what exactly need to be!
TomD
October 12, 2021, 6:22pm
6
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.