[Issue] Rounding - Calc field

Questions related to using nuBuilder Forte.

[Issue] Rounding - Calc field

Postby toms » Fri Feb 16, 2018 12:26 am

Hi,

A calc field calculates the sum of hours divided by 8.4.

75.60 / 8.4 = 9.00 but 8.99 is displayed. How to show the exact/accurate value?

calc_rounding_issue.png
calc_rounding_issue.png (15.47 KiB) Viewed 995 times
toms
 

Re: [Issue] Rounding - Calc field

Postby admin » Fri Feb 16, 2018 6:09 am

toms,

I have added Math.round().

round_added.PNG
round_added.PNG (12.55 KiB) Viewed 989 times


You'll need to get the latest from Github and then Update.


Steven
admin
Site Admin
 
Posts: 3289
Joined: Mon Jun 15, 2009 9:53 am

Re: [Issue] Rounding - Calc field

Postby toms » Fri Feb 16, 2018 1:20 pm

Steven,

Thanks for adding the Round() function. That has solved my rounding issue.

Edit: Now figures are rounded where no rounding should happen (e.g. 27.60 / 8.4 is rounded to 3 but it should be output as 3.33)
toms
 

Re: [Issue] Rounding - Calc field

Postby toms » Fri Feb 16, 2018 2:04 pm

Steven,

The rounding could be fixed with toFixed().

I did some testing:

var v = 0;
var u = 8.4 * 9;
v = parseFloat(Number(v) + Number(u)).toPrecision(10)

var t = v / 8.4;
// 75.60000000 / 8.4

print(t); // 8.999999 <--- incorrect rounding

t = + t.toFixed(2); // 2 to be replaced with the precision of the calc field ( data-nu-format="N|1000.00" )
print(t); // 9 <-- correct rounding

Test it for yourself:

http://rextester.com/GIF55847
toms
 

Re: [Issue] Rounding - Calc field

Postby admin » Fri Feb 16, 2018 2:55 pm

toms,

Adding rounding was a good idea.

For decimal places, people can always do this...

decimal_places.PNG
decimal_places.PNG (15.06 KiB) Viewed 979 times


If someone needs something more complicated they can use Javascript like the rest of us.

Steven
admin
Site Admin
 
Posts: 3289
Joined: Mon Jun 15, 2009 9:53 am

Re: [Issue] Rounding - Calc field

Postby toms » Fri Feb 16, 2018 4:41 pm

Steven,

When you are dealing with things that are counted, rounding errors look "ugly" when you expect a whole number without fractions.
In my case, 8.9999 is not only arithmetically wrong but irritating. The correct result is 9 days, nothing more or less.
In accounting, when dealing with financial figures, it might be a different story.

Having said that, your last post gave me an idea.

This formula seems to do the job:

nuTotal('sub_dates.hours') / 8.4 + 0.000000000000001
toms
 

Re: [Issue] Rounding - Calc field

Postby admin » Sat Feb 17, 2018 5:13 am

.
admin
Site Admin
 
Posts: 3289
Joined: Mon Jun 15, 2009 9:53 am


Return to General



cron