Rounding a calculation to the nearest 0 or 5
-
I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
"); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance. -
I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
"); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance.Use
Math.round( n /5) * 5;
where n is the number you are looking to round off.
Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com
-
Use
Math.round( n /5) * 5;
where n is the number you are looking to round off.
Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com
That did not work but looking at my email again, the entire script didn't paste. It is (Math.ceil(CourseHours/40)+"
"); Sorry about that. Thanks much. -
That did not work but looking at my email again, the entire script didn't paste. It is (Math.ceil(CourseHours/40)+"
"); Sorry about that. Thanks much.I guess your entire script is not there again... Anyways... Math.ceil will always take it to next higher number whereas Math.round round it to the nearest integer. So you can choose as per your requirement. From what I think you are looking to do is - You require (CourseHours/40) rounded to nearest 0 or 5... i.e. the number you eventually looking to round off is the output of - CourseHours/40... So for my solution to work - 'n' should be substituted by - CourseHours/40 i.e
Math.ceil(CourseHours/40/5) * 5
or use Math.round as per your requirement. I hope this helps.
Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com
-
I created a PDF form from an excel file. I have everything figured out, except one of the fields in which I am able to round the answer up to the nearest integer, but I need to take it one step further and round it to the nearest 5 or 0. This is what I an using as my custom calculation script: (Math.ceil(CourseHours/40) + "
"); I have just started learning javascript so I found this off of a website. If anyone could help I would certainly appreciate it. Thank you in advance.Hi, Try Math.floor() Thanks,
-
I guess your entire script is not there again... Anyways... Math.ceil will always take it to next higher number whereas Math.round round it to the nearest integer. So you can choose as per your requirement. From what I think you are looking to do is - You require (CourseHours/40) rounded to nearest 0 or 5... i.e. the number you eventually looking to round off is the output of - CourseHours/40... So for my solution to work - 'n' should be substituted by - CourseHours/40 i.e
Math.ceil(CourseHours/40/5) * 5
or use Math.round as per your requirement. I hope this helps.
Imagination is the one weapon in the war against reality!!! aniruddhaloya.blogspot.com
It seems to work, but what I am finding is that, as most of the answers will be less than 1, it is rounding it to the nearest 0 or 5 in the thousandths when I need it to be no larger then hundreths. SO all I should have as the final answer is .15 or .35. Does that make sense?
-
It seems to work, but what I am finding is that, as most of the answers will be less than 1, it is rounding it to the nearest 0 or 5 in the thousandths when I need it to be no larger then hundreths. SO all I should have as the final answer is .15 or .35. Does that make sense?
You can try using "Number".toFixed(x) along with the existing solution for fixing that. More info of your data set may help to find a better solution though :)
Imagination is the one weapon in the war against reality!!! http://aniruddhaloya.blogspot.com