Skip to main content
alexandre95474313
Known Participant
November 16, 2016
Question

Help with javascript rounding

  • November 16, 2016
  • 3 replies
  • 5350 views

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.

Thanks in advance!

fod

This topic has been closed for replies.

3 replies

Inspiring
November 19, 2016

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

alexandre95474313
Known Participant
November 27, 2016

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.

Inspiring
November 27, 2016

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.

Inspiring
November 16, 2016

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.

Inspiring
November 16, 2016

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);

})();

alexandre95474313
Known Participant
November 16, 2016

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!

Inspiring
November 16, 2016

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.