Welcome to the nuBuilder Forums!

Register and log in to access exclusive forums and content available only to registered users.

[Issue] Rounding - Calc field

Questions related to using nuBuilder Forte.
Locked
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

[Issue] Rounding - Calc field

Unread post by toms »

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
You do not have the required permissions to view the files attached to this post.
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: [Issue] Rounding - Calc field

Unread post by admin »

toms,

I have added Math.round().
round_added.PNG
You'll need to get the latest from Github and then Update.


Steven
You do not have the required permissions to view the files attached to this post.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: [Issue] Rounding - Calc field

Unread post by toms »

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
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: [Issue] Rounding - Calc field

Unread post by toms »

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
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: [Issue] Rounding - Calc field

Unread post by admin »

toms,

Adding rounding was a good idea.

For decimal places, people can always do this...
decimal_places.PNG
If someone needs something more complicated they can use Javascript like the rest of us.

Steven
You do not have the required permissions to view the files attached to this post.
toms
Posts: 785
Joined: Sun Oct 14, 2018 11:25 am

Re: [Issue] Rounding - Calc field

Unread post by toms »

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
admin
Site Admin
Posts: 2814
Joined: Mon Jun 15, 2009 2:23 am
Been thanked: 25 times

Re: [Issue] Rounding - Calc field

Unread post by admin »

.
Locked