Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Rounding single non-calc field

Participant ,
Dec 18, 2019 Dec 18, 2019

I have created a financial statement form with format of 0 decimals.  I know that formatting is visual only.  The table has five columns of information relating to Rental Property #1, #2, etc.  One of the calcs is the following (which I repeat for each property):

event.value = Math.round(this.getField("GrossMthRent_1").value * this.getField("OwnershipPercentage_1").value);    which I contain in a hidden field (Prop1Rent),etc.  Then I add all Prop#Rent calcs for a visable TotalShareMonthRent.  This total then flows to the summary page where it is presented as an annualized total (TotalShareMonthRent*12).   The calculation order was challenging as the totals on page two and three flow to summary on page one so 2&3 have to be calc'd before page 1, but it works great and  both the format and the actual system number are the same. 

 

My last hurdle is that I don't know how to structure a single field in the same way (formt=result), or anohter way would be to stop a user from inputing decimal values.  Example:

CashA - input $1.25, formats to $1

CashB - input $1.25, formats to $1

CashC - input $1.25, formats to $1

A+B+C Total calc = $3.75, formats to $4, but the individual formats equal $3. Visually, the section does not balance on the printed document.

I would like to either block the user from inputting a value with decimals, or have a script that rounds each line level entry the same as it does for calculations.

How can I do this?  I'm not looking forward to touching each user input field again but once its done, its done!

TIA ~ Michelle

TOPICS
Acrobat SDK and JavaScript
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2019 Dec 18, 2019

Hi,

The Math.round function rounds a number to the nearest integer.

• for a decimal number <.5, the number is rounded down (so 1.25 is rounded to 1)

• for a decimal number >=.5, the number is rounded up (so 3.75 is rounded to 4)

 

For limiting a number to an integer:

• for an unlimited number, you just have to control the key you are clicking, so put this script in "custom key script":

var regxp=/^[0-9]$/;
if(!event.willCommit) {
if(event.change.length>0 && regxp.test(event.change)==false) event.rc=false;
}

• for a limited number (5 for example here below), you also have to check and limit the number, so the script will be:

if(!event.willCommit) {
var regxp=/^[0-9]$/;
var aRslt=event.value.split("");
aRslt.splice(event.selStart, event.selEnd-event.selStart, event.change);
var strTest=aRslt.join("");
var rpat=/^\d{0,5}$/;
event.rc=rpat.test(strTest) && (event.change=="" || regxp.test(event.change));
} else {
var rpat=/^\d{0,5}$/;
event.rc=rpat.test(event.value);
}

 

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2019 Dec 19, 2019

A simple way to round a number to a set number of decimal places is to use same print function that's used to format numbers:

var b = 4.75555;

var a = Number(util.printf("%0.3f",b));

 

"b" will be rounded to 3 decimal places so that a=4.756

 

To automatically set the value of a field with this technique, place the code into a validation script;

 

event.value = Number(util.printf("%0.3f",event.value ));

 

All user entries will be rounded to the specified number or decimals.

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2019 Dec 20, 2019
LATEST

A simple way... when we master the util.printf method! 🙂

@+

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines