Skip to main content
Participant
January 22, 2019
Answered

Odd to even rounding script

  • January 22, 2019
  • 3 replies
  • 910 views

We utilize a non conventional way of rounding and I would like to format the calculated fields in my form to this rounding method:

Example: 0.45 rounds to 0.4

                0.35 rounds to 0.4

It's called "Odd to Even" rounding. If the place being rounded (tenths place) has a five in the hundredths place and the number in the tenths place is even it rounds down and if the number in the tenths place is odd it rounds up.

Any help would be greatly appreciated. Thanks!

This topic has been closed for replies.
Correct answer markt31311181

Thank you Thom Parker and George_Johnson​! Your direction has solved my issue. ​That link George shared did the trick once Thom pointed out that it had to be a document script. Then all I had to do was utilize that function in the custom formatting like I would anything else.

3 replies

markt31311181AuthorCorrect answer
Participant
January 23, 2019

Thank you Thom Parker and George_Johnson​! Your direction has solved my issue. ​That link George shared did the trick once Thom pointed out that it had to be a document script. Then all I had to do was utilize that function in the custom formatting like I would anything else.

Thom Parker
Community Expert
Community Expert
January 23, 2019

Glad you got all the pieces together. The custom format script is often overlooked.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
January 22, 2019

It's commonly known as "Banker's rounding", which may be helpful if you do a search for JavaScript code that accomplishes it, e.g.: Gaussian/Banker's Rounding in JavaScript · GitHub

Thom Parker
Community Expert
Community Expert
January 22, 2019

You'll need to write your own rounding function, and custom calculation scripts. Do you have any programming experience?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Participant
January 22, 2019

Not really. I have just begun delving into pdf programming this month. I've done a few things like auto imputing a future date and basic custom calculation. I'm learning as I go.

Thom Parker
Community Expert
Community Expert
January 22, 2019

The rounding function should be a document script, so it's defined once and accessible to all other scripts in the PDF.

Did you want the actual value of the field to be rounded? or just how the value is displayed?  This is important if the value is used in other calculations.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often