View Full Version : Javascript Math Calculation in Adobe PDF

wwilson80

05-27-2012, 12:12 AM

I am trying to change a number in a calculation in a PDF document. The number comes out to 8000.536 I need the number to be 8000.54. I have the number format set to 2 decimal places. I have tried the round function and it does not seem to work. I have a field called CPP which is a inputted whole number. The second field is called Deposit which is a calculated field as follows CPP * .25 the next field is called Balance which is a calculation of CPP - Deposit but depending on the amount in CPP the return calculation is off .01 which is because the Deposit field is not rounded off. How can I correct this please.

EpicWebDesign

05-27-2012, 12:41 AM

Can you post your code? The math.round function should work.

for example:

var myNum=8000.536

var result=Math.round(myNum*100)/100

that should return 8000.54

wwilson80

05-27-2012, 12:58 AM

var myNum=this.getField("Deposit")

var result=Math.round(myNum*100)/100;

CPP - result

The field that this code is in is called Balance_Due. If I use the fields CPP - Deposit I am always off .01.

EpicWebDesign

05-27-2012, 01:03 AM

Please post the whole code (JS & form). From what you posted, CPP has not been assigned a value. If I can see the whole code I can try to help you figure out where the disconnect is.

wwilson80

05-27-2012, 01:08 AM

The CPP field code is an addition function as follows:

RS3$, Sales_Tax, Subtotal

The Deposit field code is as follows:

CPP * .25

Thank you for your help

EpicWebDesign

05-27-2012, 01:39 AM

I'm assuming you are using custom calculations. Have you tried specifying the fields are numeric? See if this makes any difference? You might need to do the same thing for the CPP field.

var myNum=Number(this.getField("Deposit").value);

var result=Math.round(myNum*100)/100;

event.value = CPP - result;

wwilson80

05-27-2012, 01:47 AM

I get a syntax error on the second line of code

EpicWebDesign

05-27-2012, 01:51 AM

On the result=math... line?

Please forgive my slow answers... I'm posting from my phone :)

wwilson80

05-27-2012, 02:05 AM

Yes in the math.round line

EpicWebDesign

05-27-2012, 02:16 AM

Ok, but this didn't occur when you didn't specify it as numeric, correct? i.e.,

var myNum=this.getField("Deposit")

var result=Math.round(myNum*100)/100;

have you tried doing ONLY that function (without the event.value =CPP-results)? Does it round correctly?

wwilson80

05-27-2012, 02:32 AM

It still gives a syntax error

EpicWebDesign

05-27-2012, 02:35 AM

var myNum=this.getField("Deposit")

var result=Math.round(myNum*100)/100;

CPP - result

The field that this code is in is called Balance_Due. If I use the fields CPP - Deposit I am always off .01.

Sorry, I thought you said you were using the code above but it simply wasn't rounding off the numbers correctly. Has it been returning a syntax error the entire time? You hadn't mentioned that until now. I'm just trying to narrow down where the problem lies.

wwilson80

05-27-2012, 02:38 AM

It always returns a syntax error when using the round.math function

EpicWebDesign

05-27-2012, 02:43 AM

Oh... ok. I was not aware of that until now. What version of Adobe are you using?

wwilson80

05-27-2012, 02:49 AM

Acrobat 9 Professional

EpicWebDesign

05-27-2012, 03:01 AM

hmm... math.floor and math.ceil will round down and up respectively. Have you tried either one to see if they return syntax errors?

EDIT: I have to go, but will check in tomorrow to see if you've made any progress. Sorry I couldn't solve your issue this evening, but I'm still willing to help. If you can, please copy/paste your code for ALL 3 fields here so I can make sure there isn't a typo or anything that is causing the issue.

wwilson80

05-27-2012, 03:34 AM

var Mynum = this.getField("Deposit");

var ww = this.getField("CPP");

var result = Math.round(Mynum.value*100)/100;

ww - result;

This code does not give me a syntax error but also does not give me a value

EpicWebDesign

05-27-2012, 04:01 AM

Have you tried calling the value using

event.value=ww-result;

wwilson80

05-27-2012, 03:40 PM

That did not work also. Let's go back to square one:

The field CPP is a sum of fields BP + RS1 + ST which equals the value of 100,810.11

The field Deposit is computed as CPP *.25 which gives a value of 25,202.525 which rounds up to 25,202.53

The field Balance is computed as CPP - Deposit which gives a value of 75,607.58.

I am trying to round the field Deposit so that the answer of field Balance comes to 75,607.57.

wwilson80

05-27-2012, 03:43 PM

I am sorry the total of CPP is 100,810.10 not 100,810.11

EpicWebDesign

05-27-2012, 06:37 PM

Ok, just to confirm, you ARE using Custom Calculations for this, correct? Have you tried removing the commas from the numbers to see if that makes any difference? It doesn't make sense that the Deposit field is rounding correctly, yet the end result is off by .01 when you run the subtraction (CPP-Deposit), so we need to figure out where the numbers are changing....

Try this (in Custom Calculations mode) and see what happens:

var Mynum = this.getField("Deposit");

var ww = this.getField("CPP");

var result = Math.round(Mynum.value*100)/100;

event.value = ww.value - result.value;

wwilson80

05-27-2012, 06:52 PM

I have the Deposit Field to a 'Number' with 2 decimal places this comes out to be $25,200.03, however is you increase the decimal places to 3, the number becomes $25,200.025. This field must be round to a positive number with only 2 decimals ie $25,200.03. When the Balance_Due field calculates the CP - Deposit function is it using the $25,200.025 which gives the .01 difference.

wwilson80

05-27-2012, 06:54 PM

When I use the latest code you provided it gives the error:

The value entered does not match the format of the Field [Balance_Due]

EpicWebDesign

05-27-2012, 07:05 PM

What is for format set for the Balance_Due field?

wwilson80

05-27-2012, 07:06 PM

the format is number with 2 decimals

EpicWebDesign

05-27-2012, 07:13 PM

I don't know if this will work, but it's worth a try:

var Mynum = this.getField("Deposit");

var ww = this.getField("CPP");

var result = Math.round(Mynum.value*100)/100;

var result2 = result.toFixed(2);

event.value = ww.value - result2.value;

EDIT: also, just to be sure... the CPP amount never has more than 2 decimals, correct?

wwilson80

05-27-2012, 07:17 PM

I get the same error message that it gave me for the field Balance_Due

wwilson80

05-27-2012, 07:20 PM

The problem is that when 100810.10 is multiplied by 25% it gives an answer of 25,202.525 not 25,202.53 which is the value of 'Deposit'.

EpicWebDesign

05-27-2012, 07:20 PM

This is weird... I don't mean to back-track, but let's try a few things to see if we can narrow down the problem.

First, let's try it without doing the subtraction, just to make sure the Math.round function is doing what we need. So try this:

var Mynum = this.getField("Deposit");

var result = Math.round(Mynum.value*100)/100;

event.value = result.value;

Does that return the correctly formatted/properly rounded number?

Update: just saw your last post. Does the Deposit field allow for 3 decimals? If not, that may be where our problem lies.

wwilson80

05-27-2012, 07:22 PM

I am using 'Simplified Field Notation' caluclation for the Deposit entry and 'Custom Calculation Script' for the Balance_Due

wwilson80

05-27-2012, 07:26 PM

Which field do you want me to enter the latest code into? I entered the code into the 'Custom etc.' box and it does not give me any amount when I clear the pdf and run the numbers again.

wwilson80

05-27-2012, 07:29 PM

I changed the 'Balance_Due' field to 3 decimals and it still does not give me an amount

EpicWebDesign

05-27-2012, 07:30 PM

Maybe we should try using Custom Calculation for the Deposit field and calling it as follows:

var ww = this.getField("CPP");

var ww2 = ww.value * .25;

var result = Math.round(ww2.value*100)/100;

event.value = result.value;

EpicWebDesign

05-27-2012, 07:35 PM

I am using 'Simplified Field Notation' caluclation for the Deposit entry and 'Custom Calculation Script' for the Balance_Due

I think that might be the problem. Simplified won't allow us to round up/down the value of CPP *.25 before printing it to the Deposit field. That's why I suggested changing the Deposit field to Custom and using the code above to see if you get a proper result in the Deposit field.

wwilson80

05-27-2012, 07:35 PM

I entered the code into the field Deposit "Custom Calculation Script". I changed the Balance_Due Field to a 'Simplified Field Notation' code of CPP - Deposit. When I cleared the form and re-inputted the numbers. The Balance_Due Field had the same amount as the CPP field. There was no calculation done in the 'Deposit' field.

EpicWebDesign

05-27-2012, 07:40 PM

I entered the code into the field Deposit "Custom Calculation Script". I changed the Balance_Due Field to a 'Simplified Field Notation' code of CPP - Deposit. When I cleared the form and re-inputted the numbers. The Balance_Due Field had the same amount as the CPP field. There was no calculation done in the 'Deposit' field.

So nothing showed up in the Deposit field at all? Or the number appeared correctly but the calculation just didn't work?

wwilson80

05-27-2012, 07:43 PM

The deposit field had no numbers in it at all

EpicWebDesign

05-27-2012, 07:50 PM

Ok, let's try it again, using different variable names (just to rule that out as the issue) and add some brackets to our calculation field. We've tried so many options for this that I can't keep track of what we've used.

var Calc_A = this.getField("CPP");

var Calc_B = (Calc_A.value * .25);

var Final = Math.round(Calc_B.value*100)/100;

event.value = Final.value;

Let me know what, if anything, shows up in the Deposit field.

wwilson80

05-27-2012, 07:59 PM

Copied the code into deposit Custom Calculation Script and it does not give any number.

EpicWebDesign

05-27-2012, 08:03 PM

Ok... god I wish I was at my office where I have Acrobat installed! I'm trying to do all this from memory on my laptop at home :)

See what happens if you run the same code without the Math.round line (assuming the Deposit field is still set to number + 3 decimals).

var Calc_A = this.getField("CPP");

event.value = (Calc_A.value * .25);

Just changed the code a bit... Use the 2-line code now shown and let me know the results

wwilson80

05-27-2012, 08:07 PM

Gave an answer of 25202.525

EpicWebDesign

05-27-2012, 08:11 PM

Good... we're getting a result now, so let's expand it a bit. let me know what, if anything you get with this:

var Calc_A = this.getField("CPP");

var Calc_B = (Calc_A.value * .25);

event.value = Calc_B.value;

Hopefully, you will get the same number and then we can expand it further.

wwilson80

05-27-2012, 08:14 PM

It returned no value whatsoever

EpicWebDesign

05-27-2012, 08:18 PM

Damn! Ok, let's try it with the 2 lines again, but with more calculations. Basically what I'm trying to do is create a work-around for the Math.round function which doesn't seem to work in this situation.

var Calc_A = this.getField("CPP");

event.value = ((Calc_A.value * .25)*100)/100;

wwilson80

05-27-2012, 08:22 PM

Gives the answer 25202.525

EpicWebDesign

05-27-2012, 08:27 PM

UGH! This is frustrating me as much as I'm sure it is to you!! I'm trying to think outside the box here to come up with a solution.... Here's another random thought to try:

var Calc_A = this.getField("CPP");

event.value = Math.round((Calc_A.value*.25)*100)/100;

EDIT: or maybe this

var Calc_A = (this.getField("CPP")*.25);

event.value= Math.round(Calc_A.value*100)/100;

wwilson80

05-27-2012, 08:29 PM

I changed your code to the following and it worked:

var Calc_A = this.getField("CPP");

event.value = Math.round((Calc_A.value * .25)*100)/100;

I want to thank you so much for your help I really appreciate it.

EpicWebDesign

05-27-2012, 08:32 PM

LMAO I just posted that exact same code in my post above :) So glad we finally solved this!

wwilson80

05-27-2012, 08:37 PM

I am glad we solved it also, again thank you for you help and patience. Have a great weekend.

EpicWebDesign

05-27-2012, 08:43 PM

And a big thanks to you for your patience with me! I had a hard time wrapping my mind around this one since I didn't have the software here at home to test with! Just glad we worked it out. I'm gonna celebrate and have a beer and enjoy the sun (it's 85 degrees out today). Hope you have a great weekend too.

Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.