Skip to main content
Participant
Dec 6, 2017 12:27 AM UTC
Question

How do I get my form calculation to round to 2 decimal places?

Hi,

I need my total value amount in my form to display two decimal places. At the moment it shows as .00 no matter what numbers I put in.

The script I am using is this:

(function () { 

 

    // Get the field values, as numbers 

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

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

 

    if (v1 !== 0) { 

        event.value = v1 * v2 * 220; 

    } else { 

        event.value = ""; 

    } 

 

})();

Please help! Thanks in advance.

This topic is closed to new replies. Start a new post to keep the conversation going.

4 replies

Inspiring
Dec 6, 2017 12:36 AM UTC

Change that one line to:

event.value = util.printf("%.2f", v1 * v2 * 220);

If that doesn't work, please give examples of the values for the ccc and ddd fields, along with the displayed result.

Translate
Participant
Dec 6, 2017 12:44 AM UTC

Hi, thanks for your reply but that didn't work.

ccc is calculated by:

(function () { 

 

    // Get the field values, as numbers 

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

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

 

    if (v2 !== 0) { 

        event.value = v1 / v2 ; 

    } else { 

        event.value = ""; 

    } 

 

})();

So i have aaa, bbb, ccc, ddd, eee. On my phone in Reader when these equal 65, 1.35, 48, and 25 respectively I get eee to equal 264,814.8

On my desktop when these same values are entered eee displays as 264,000.00

Translate
Thom Parker
Community Expert
Community Expert
Dec 6, 2017 1:30 AM UTC

What do you have set as a format on the calculated fields? I'd suggest removing all formatting. Then let us know what you  got.

Sounds like Reader is having an issue. Could be extra code that's in there.

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often