7 Replies Latest reply on Dec 10, 2010 3:45 PM by RoC1909

Question regarding ROUNDING a number to nearest Quarter

Hello all,

Please forgive me if I am not posting this in the appropriate area, but I am not sure which area it could go into (scripting, Javascript, Designer...) and the Acrobat Scripting forum seemed like as good a place as any.

I have field that allows a user to enter a 2 digit number with 2 decimal places. (i.e 12.30 or 9.78 up to 24.00. This field corresponds to the hours in a day). What I would like is to "round" to the nearest quarter hour. As a example, if a person enters:

22.15 - the field would round UP to 22.25

9.78 - the field would round DOWN to 9.75

12.10 - the field would round DOWN to 12.00

.15 - would round UP to .25

.96 - would round to 1.00

.01 to .24 - would round UP to .25

In essence, any number they place into this field (up to 24.00) would round appropriately.

I looked at the FormCalc ROUND function and DATE / TIME functions, but it doesn't look like any of those would allow me to do what I need to do. I imagine it would have to be in JavaScript but again, I am not sure.

If anyone can point me in the proper direction, it would be greatly appreciated and hopefully, I explained what I want to do correctly.

Jon

• 1. Re: Question regarding ROUNDING a number to nearest Quarter

You description is not consistent. Why would 12.10 round down and 0.01 to

0.24 (which includes 0.10) would round up?

• 2. Re: Question regarding ROUNDING a number to nearest Quarter

Here's a sample code I wrote to round a number (x) to the nearest quarter (new_x):

var x = 22.15;
var rounded_x = Math.floor(x);
var new_x;

if (x-rounded_x<=0.125) new_x = rounded_x; // 0 - 0.125 round down to 0
else if (x-rounded_x<=0.375) new_x = rounded_x+0.25; // 0.125 - 0.375 round to 0.25
else if (x-rounded_x<=0.625) new_x = rounded_x+0.5; // 0.375 - 0.625 round to 0.5
else if (x-rounded_x<=0.875) new_x = rounded_x+0.75; // 0.625 - 0.875 round to 0.75
else new_x = rounded_x+1; // 0.875 - 1.00 round up to 1

Edit: improved the code...

• 3. Re: Question regarding ROUNDING a number to nearest Quarter

Ugh....stupid me. My brain was thinking of one thing, and my fingers of another. Please disregard that last example.

• 4. Re: Question regarding ROUNDING a number to nearest Quarter

Thought so...

(see my second post to this thread)

• 5. Re: Question regarding ROUNDING a number to nearest Quarter

Your code has really got me thinking (or the fact that I was OVER thinking this problem) and it gave me that "push" that I needed to tackle this.

• 6. Re: Question regarding ROUNDING a number to nearest Quarter

A simple function like:

function MyRound(nValue, nRound) {
return Math.round(nValue / nRound) * nRound;
} // end Myround

Will compute the round value to the nearest nRound value. This function would not limit one to only the nearest 0.25 but could be any value like 1,000 or 0.01.

To round field "Text13' the custom calculation script could be:

event.value = MyRound(this.getField('Text13').value, 0.25);

or without the function:

event.value = Math.round(this.getField('Text13').value / 0.25) * 0.25;