Skip to main content
chrislee123
Participating Frequently
February 20, 2019
Answered

How to Hide Trailing Zeros in a Number / Rounding to Nearest Quarter Hour

  • February 20, 2019
  • 1 reply
  • 1668 views

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.

This topic has been closed for replies.
Correct answer try67

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);

1 reply

try67
Community Expert
Community Expert
February 20, 2019

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);

}

chrislee123
Participating Frequently
February 21, 2019

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.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
February 21, 2019

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);