Skip to main content
Participant
May 28, 2020
Answered

Round Answer to a Formula Entered into a Fillable pdf

  • May 28, 2020
  • 2 replies
  • 9130 views

 

Hello,

This is my first time attempting to write a formula in a fillabe pdf.  I've created fillable forms before but this time would like people to be able to enter scores and for the scores to calculate across and then the those totals be calculated down.  I've attached a copy of what the form itself looks as well as the fields I've entered in the form so far.  I've entered the following formula in the Simplified field notation on the Calculation tab:

              (MBR1Q1+MBR2Q1+MBR3Q1)/3

I want to make this automatically round to the nearest hundredths.  Any and all assistance is greatly appreciated. Thanks.

 

Correct answer Thom Parker

Thom, 

What I want it to do is:

Ex1: 25 + 26+ 25/3= 25.333333333333; I want 25.33 to show for answers like this.

Ex2: 28 + 25 + 30/3= 27.66666666667; I want it to show 27.67

Ex3: 3.5 + 3 + 3/3= 3.166666666666667; I want it to show 3.17

Ex4: 20.33 + 30.33 + 60.33/3= 36.9966666666666; I want it to show 37

I hope this explanantion helps.


If all you want to do is display the rounded value, then use the built-in formatting. 

If the actual value needs to be rounded, then you'll to uses script to modify the actual value.

It is up to you. 

 

Let me elaborate. All fields have whats called an appearance. This is text and graphics that are visible to the user. But fields also have an underlying value. If the value of a field is to be used somewhere else, for example in a calculation, then you probably don't want it to be rounded. But people often want the value that is displayed to the user to be rounded, so it looks nice. This is what formatting is about. Formatting only affects the appearance. 

2 replies

ls_rbls
Community Expert
Community Expert
May 29, 2020

You're right Try67.

 

Thank you for clarifying as usual.

 

It visually rounds it but when you click on the field it displays the real value.

 

Ok, so onlyonekj,

 

in that case, you can leave your simplified notation, but  open the properties on that  calculated field again and click on the "Format" tab.

 

Select custom, and click on the Edit button for the  Custom Format Script section.

 

You can use a very simple custom format script like this:

 

 

 

var v = Math.abs(+getField("myField").value/100)*100;

event.value = (s.toFixed(2);

 

 

 

Thom Parker
Community Expert
Community Expert
May 29, 2020

This also does not round the value. 

The Format script only changes the appearane of the field value. It does not change the value itself.

Do you really need the value rounded? or is this only for display?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
ls_rbls
Community Expert
Community Expert
May 29, 2020

Thank you also Thom for clarifying this.

 

I tried Math.round and Math.ceil.

 

I used a calculator to compare the results from the javascript.

 

I entered in one field 3.65 and in another 4.71 and then multiply them.

 

Using a calculator it equals 17.1915

 

In javascript, using Math.round for the whole equation rounds up the number to 18.

 

While using Math.ceil rounds it up to 17.

 

Using Math.abs by itself displays the whole real value. Way too big number.

 

What do yo recommend? 

 

I don't know how to work around this.

 

 

 

ls_rbls
Community Expert
Community Expert
May 29, 2020

Hi,

 

Just right-click on the total text field >>>>Properties>>>> and go to the "Format" tab. 

 

From the dropdown menu provided select "Number". 

 

You'll notice that as soon as you select this option, you will get access to how many decimal spaces you want to the right of the period.

 

Selecting "0" will allow the rounding of this number to the nearest hundredth.

 

If this is not what you want to do then  you can add a custom format javascript for this field.  to control this rounding.

try67
Community Expert
Community Expert
May 29, 2020

This is NOT rounding. Setting the Format setting of the field doesn't change its actual value, just the value that is displayed. If the result is 1.5 and you set it to show zero decimals it will show as "2", but if you then multiply the value of that field by 2 it will result in 3, not 4.

To really round the value of the script you would need to use a script.