Copy link to clipboard
Copied
I have fields on a PDF form for number of hours. I want to allow for 2 decimal places to allow for quarter hours (15 min, 45 min). This is an example of what is currently being displayed:
4.00
3.25
6.50
Is there a validation script that would hide the trailing zeros? I want the numbers above to be displayed as:
4
3.25
6.5
I've also been looking for a script that would round to the nearest quarter hour (.25, .5, .75), but I haven't had any luck finding this on forums. Is this even possible? It seemed too complicated so I abandoned the idea. I'm a novice when it comes to custom scripts and would appreciate any help from this knowledgeable community.
Place the code as a doc-level script and as field's custom validation (or as a part of the calculation) script, enter this:
event.value = roundToNearestQuarter(event.value);
Copy link to clipboard
Copied
This function I wrote will do it:
function roundToNearestQuarter(v) {
v = (Math.round(v * 4) / 4);
if (v==Math.round(v)) return v.toFixed(0);
else return v.toFixed(2);
}
Copy link to clipboard
Copied
Thanks for your reply. It wasn't able to get this to work. Could you please advise where I should add this?
Is there a stand-alone code just to get rid of trailing zeros after the decimal? I have use for this on a different form I'm working on.
Copy link to clipboard
Copied
Place the code as a doc-level script and as field's custom validation (or as a part of the calculation) script, enter this:
event.value = roundToNearestQuarter(event.value);
Copy link to clipboard
Copied
Thank you! Works perfectly for rounding to the nearest quarter. Is there anything I can add to get rid of the zeros at the end?
Copy link to clipboard
Copied
It does that, too.
Copy link to clipboard
Copied
I had to change the format of the field from Number to None to get this to work. Anything that rounds to ".5" still displays as ".50", but it works for all whole numbers and quarters. Now if I type something other than a number, it displays "NaN". Is there a way to only allow for a number to be added?
Copy link to clipboard
Copied
You didn't say anything about removing the zero from "0.50", only full numbers.
And if you want to use this script and have non-numeric values then you'll need to first check if the value can be converted to a number, and if not, not call the function.
Copy link to clipboard
Copied
In my original post, I gave the example of 6.50 displaying as 6.5.
I don’t want non-numeric values at all, but the function script wouldn‘t work when I formatted the field as a number. I’m not sure how to add the “if” statements you described. Sorry, I’m a newb. I appreciate your patience in helping me through this.