Copy link to clipboard
Copied
Hi, I'm a novice at JavaScript so please excuse me if my question is a bit simple.
In a PDF form, using Acrobat 9 Pro I'm trying to preform some mathematical calculations in a custom calculation script and I've hit a brick wall
I have a number in a variable, lets say 3.7. I want to return the whole number component, the "3" without rounding it up or down. I know I've done this in the past in AccessBasic, but I'm at a loss as to how to achieve it in JavaScript
I tried using toFixed(0) but that still round up. I've searched various JavaScript references but I'm unable to find a function that would help me.
The reason I'm trying to do it is rather convoluted, so I'm trying not to confuse you all by entering a long winded description of the problem. Any pointers would be gratefully received.
1 Correct answer
Try it in the JavaScript debugger.
3.2 - 0.5 is 2.7, this will be rounded to 3
Copy link to clipboard
Copied
Use something like this:
var a = 3.7;
Math.round(a - 0.5);
Copy link to clipboard
Copied
Bernd,
Thank you for responding. Based on your answer, what happens is the value is say 3.2? That would then produce a result of 2 which is not what I'm after.
For example what I want is
40.6 Result = 40
40.1 Result = 40
40.9 Result = 40.
I just want to trim the number to it's whole value without rounding up or down.
Copy link to clipboard
Copied
Try it in the JavaScript debugger.
3.2 - 0.5 is 2.7, this will be rounded to 3
Copy link to clipboard
Copied
I would use the Math.floor() method.
var a = 3.7;
Math.floor(a)
For a calculated field, one could add the following code after the calculation sets the field value:
event.value = Math.floor(event.value);
This will truncate the decimal value or round the number down to the next lower integer.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor
Copy link to clipboard
Copied
Could you please help me?
Need help with PDF form.
I want Field 3 to take average of Fields 1 and 2, and display unrounded number.
Settings:
Field 1 - Number, zero decimal places
Field 2 - Number, Zero decimal places
Field 3 - Average, Field 1 and Field 2 selected.
My problem with these settings is that Field 3 shows the unrounded average and then if I click anywhere on the page, Field 3 changes to the rounded average.
- Then, I found this posted:
System.out.print( (double)(a + b) /2 );
This stops it from changing when I click on the page, but it still shows rounded.
How can I edit this script to stop rounding?
Copy link to clipboard
Copied
That code will not work in a PDF form.
If "Field 3" is set to peform an Average calculation, then remove any formatting and it will show the raw calculation value. If it is not, then there is something wrong with the form.
Post the form so we can see the details.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Thanks,
Jasmine
Copy link to clipboard
Copied
That PDF was corrupted. Could you possibly recreate the form from scratch. And then post that PDF. You might also see an improvement in the form behavior.
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Bernd & gkaiseril,
Thank you both. I've tried both methods & get exactly the result I wanted. Thank you.

