26 Replies Latest reply on Dec 15, 2016 7:24 AM by Karl Heinz Kremer

# Help with javascript rounding

Hi !

I have a problem with a form that includes an addition of 3 fields, but that gives me a "wrong" answer because unlike the "displayed value" in each field (that is rounded to 2 decimals), the Total field does not take into account the rounded sums, and hence my total is "wong" by 1 cent.

I know this is a typical problem, and yes, I have searched on MANY forums on javascript and adobe... and I have tried to paste or arrange some line of javascript, but since I don't know anything about it, I get "syntactical errors"...

So, more specifically, here are the 3 fields that needs to add up:

Regular priceSUBTOTAL

Regular priceTAX1

Regular priceTAX2

and the total field is:

TOTAL REGULAR

To give you the concrete example I have trouble with, here are the numbers:

Regular priceSUBTOTAL = 99,50\$

Regular priceTAX1 = 4,98\$ (rounded up from 4,975)

Regular priceTAX2 = 9,93\$ (rounded up from 9.925125)

the total should be: 114,41, but it displays 114,40 (an obvious mistake)

as the exact value is 114.400125

So... what should I do in order to get 114,41\$ in the Total field  ?

What line of javascript do I need to incorporate?

And where? (as a Custom Validation script or a Custom Calculation script?)

Any help will be greatly appreciated.

fod

• ###### 1. Re: Help with javascript rounding

You need to correctly round each intermediate value. You can do this if you use a custom calculation script for each calculated field, but can't if you use one of the other calculation options. As an example, here's a custom calculation script that sums three field values:

// Custom calculation script

(function () {

// Get the three input values, as numbers

var v1 = +getField("Text1").value;

var v2 = +getField("Text2").value;

var v3 = +getField("Text3").value;

// Calculate the sum

var sum = v1 + v2 + v3;

// Round to the nearest 100th and set this field value

event.value = util.printf("%.2f", sum);

})();

• ###### 2. Re: Help with javascript rounding

Also note that when one applies the rounding can have an effect on the result. If computing sales tax, round the input values for the calculation and then round the result.

I would round the values being summed to come to the subtotal before computing the sales tax.

• ###### 3. Re: Help with javascript rounding

I pasted all that script in the "Total" field, and it did help for that field: I am now seeing a value of 114,40\$ instead of 114.400125.

Now if you could only tell me what to paste in the 3 other fields, to round them up as well, maybe I could get the wanted result, which is 114,41\$ (ie 1 cent more).

So can you tell me how to round up to 2 decimals the 3 other fields please?

Many thanks!

• ###### 4. Re: Help with javascript rounding

When you say "round them up", exactly what do you mean? The code I posted round to the nearest 100th. Without knowing how those other fields are calculated, I can't suggest any specific code.

• ###### 5. Re: Help with javascript rounding

ok...

The 1st field (named Regular priceTAX1) is calculated this way: Regular priceSUBTOTAL x TAX1

The 2nd field (named Regular priceTAX2) is calculated this way: Regular priceSUBTOTAL x TAX2

And the 3rd field (where I pasted all the script you gave me as Custom Calculation script) is named TOTAL REGULAR and calculated this way: 1st field + 2nd field (or Regular priceTAX1 + Regular priceTAX2)

Overall, it is a simple payment addition: SUBTOTAL + TAX1 + TAX2 = TOTAL

Thanks George

• ###### 6. Re: Help with javascript rounding

Here's how you could do the first one:

// Custom calculation script

(function () {

// Get the two input values, as numbers

var v1 = +getField("Regular priceSUBTOTAL").value;

var v2 = +getField("TAX1").value;

// Calculate the product

var product = v1 * v2;

// Round to the nearest 100th and set this field value

event.value = util.printf("%.2f", product);

})();

Be sure to set the field calculation order to whatever makes sense for the form.

• ###### 7. Re: Help with javascript rounding

Thanks again for the reply George. I really appreciate it. And I feel we are close to the answer.

However, when I paste the code you give me, I get this error message: "The value entered does not match the format of the field [Regular priceSUBTOTAL]".

Same thing when I paste (and adapt) your script to the 2nd field, I get the same error message...

The format of the field is "Number" set at "2 decimals"... so I really don't understand why I would get this error message...

Can you give me yet again some guidance here please?

And thanks again

p.s. noob question: just to be sure... I do need to paste the "entire" script you put there right? Starting with "// Custom Calculation script"... and ending with "})();" ?

• ###### 8. Re: Help with javascript rounding

See if any errors are reported in the JavaScript console by pressing Ctrl + J. Also, temporarily set the format to None to see what the field value is being set to.

• ###### 9. Re: Help with javascript rounding

ok, so I put all the field to "Format = none".

Then I entered your script in the first field (Regular priceTAX1), and I didnt get an error message, but the result in the field is now 1.#R

Same thing with field 2...

And when I set the Format back to Number, now the result shown is 1,00\$

...

I did press Ctrl+J before I set all the fields' format to None, and here was the error message:

TypeError: f is null

1051:byteCodeTool

TypeError: f is null

1051:byteCodeTool

TypeError: f is null

1051:byteCodeTool

ReferenceError: sum is not defined

1:Field:Validate

TypeError: f is null

1051:byteCodeTool

TypeError: f is null

1051:byteCodeTool

I don't know what I am doing wrong... or why this is so complicated, but I will thank you again for your time and effort if you get the patience to guide me through this...

Thanks...

• ###### 10. Re: Help with javascript rounding

Those errors mean that the field doesn't exists, which means you didn't use the right field names in the code. When you use the correct field names, it will work. They have to match exactly,

• ###### 11. Re: Help with javascript rounding

Ok.

I changed and simplified the names of the fields to be certain there is no more errors there.

But the problem is that I am getting 1,00\$ (or 1.#R) as a result of your script now...

I pasted this script:

// Custom calculation script

(function () {

// Get the two input values, as numbers

var v1 = +getField("SUBTOTAL").value;

var v2 = +getField("TAX1").value;

// Calculate the product

var product = v1 * v2;

// Round to the nearest 100th and set this field value

event.value = util.printf("%.2f", product);

})();

Why am I getting 1,00\$ as a result?

• ###### 12. Re: Help with javascript rounding

When you get a result of 1,00\$, what is the format set to, and what are the values in the SUBTOTAL and TAX1 fields?

• ###### 13. Re: Help with javascript rounding

Format is set to Number, with 2 decimal places

The value in the SUBTOTAL field is 99,50\$

The value in the TAX1 field is 0,05

I need to multiply the SUBTOTAL and TAX1.

When I do that, I get the following value: 4,975 (which is rounded up to 4,98)

But when I put your script I get 1,00

• ###### 14. Re: Help with javascript rounding

I'd have to look at the form to see what's wrong. If you can't post it somewhere, if you want to email it to me I'd be happy to take a look: acroscript at gmail dot com

• ###### 15. Re: Help with javascript rounding

sure, I'll send you the form!

Thanks George

• ###### 16. Re: Help with javascript rounding

There is no built-in rounding function to Acrobat forms. One can use the util.printf to create a string with the decimal round or some methods of JavaScript. But I think the best approach is to create a function that can round both the decimal value or whole number of a value.

Such a document  function could be:

function myRound (n, d)
{
// souce D.P. Story, Phd.
n = String(n).replace(/,/g,"");
d = -1*d
var m = Math.pow(10,d);
n *= m;
n = Math.round(n);
n /= m;
d = ( d > 0 ) ? d : 0;
n = n.toFixed(d);
return Number(n);
} // end my round function;

One then can add a line to the end of a field's Custom JavaScript calculation like:

event.value = myRound(event.value);

Or is one of the other 2 calculation options are used, then for the Validation use a custom script like:

event.value = myRound(event.value);

sample invoice

• ###### 17. Re: Help with javascript rounding

Thanks gkaiseril,

But WHERE do I need to paste all this script? Also could you please tell me what "// souce D.P. Story, Phd." refers to?

Then you tell me to add "a line to the end of a field's Custom Javascript calculation"... but I have no idea what that script would look like that I have to put this line AT THE END OF...

Then you say "if one of the 2 other calculation options are used"... what other options are you refering to?

I know you guys are trying to help and I greatly appreciate it. But all through this thread I have yet to receive one single and simple answer. And this is all I am asking. Not "3 ways that I could do it" or "what line to add at the end of a script I have no idea how to write"...

You have to understand that I don't know anything about java......

I am just a Abode pdf user that is trying to get one addition right on a form... and I cannot believe this is so complicated. We have lost 2 weeks now on this...

So could you please tell me precisely what script I must put and where please?

Or if you think it would be simpler, I could send you the form if you just give me your email adress...

Thanks again.

• ###### 18. Re: Help with javascript rounding

I would put the function at the document level so the code is initialized when the PDF is opened and available to any action in the form. The code needs to be placed so that it is executed when the field with the calculation is updated. For custom JavaScript calculations this would be within the calculation code. For fields using "the field is the _____ of the following fields" or the simplified field notation, it could be placed in the custom validation script for the field since there is no easy way to add the script to those predefined approaches.

D.P. Story is an early evangelist for the PDF file format. He was a Mathematics Professor one of the developer or AcroTex. He was also a believe in LaTex for typesetting mathematical articles.

• ###### 19. Re: Help with javascript rounding

Could you please provide me the exact script I need to paste in the Custom Calculation please?

Or I could send you my form and you just check the 2 fields I have trouble with?

• ###### 20. Re: Help with javascript rounding

Hi George_Johnson, I didn't receive any reply from you after sending you my form (twice) at acroscript@gmail.com...

I can send it to you again, or at another address if you want.

I really need that answer, it is causing all kinds of complications for my business.

And I am certain it wouldn't take you much time to figure out what the problem is.

Thanks again.

• ###### 21. Re: Help with javascript rounding

You may want to post your file some place public, so that any of the experts here can take a look, and you don't have to rely on George's availability. We are all volunteering our time here, and sometimes we have to eat and sleep and work. As all of our lives are a bit unpredictable.

• ###### 22. Re: Help with javascript rounding

Excellent Karl!

I will do that right now!

Thanks!

• ###### 23. Re: Help with javascript rounding

Here is the link to the form: Shared Files - Acrobat.com

The Fields I have trouble with are located in the bottom right corner of the form:

- SOUSTOTAL

- TAX1

- TAX2

I need the correct TOTAL to be 114,41 (and not 114,40).

I already pasted the script that George gave me, and as a result I am getting the following error message : "The value entered does not match the format of the field [ TAX1 ] and [ TAX2 ]".

So if you could take a look at the script to see what is wrong with it, I'd really appreciate it.

Thanks again.

• ###### 24. Re: Help with javascript rounding

Take a look at the fields TPS and TVQ. You are entering data with a comma as decimal separator, but the field is not configure for that format. You need to change the field formatting rules, and then enter the data again. After these two changes, the form should work.

• ###### 25. Re: Help with javascript rounding

omg Karl you are such a savior!

It worked !!!

lol... I had almost lost hope...

It is weird that the "comma" doesn't work as decimal separator there, but it did work elsewhere...

In any event, BIG thanks to you, to George and to gkaiseril for your time and contribution.

I will correct the form and mark this problem as solved.

Thanks

• ###### 26. Re: Help with javascript rounding

The comma only works if you set the correct field format. The default is that numbers use a decimal point. You've changed the format for the other fields that I've looked at, so these two fields returned strings, not numbers, and when you try to convert a string to a number, you end up with a "NaN" value, which stands for "Not a Number". The fields that you tried to assign that NaN to were numeric fields, and a NaN is by definition not a number, so the numeric fields complained about the format not being correct.