Skip to main content
Participant
December 6, 2017
Question

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

  • December 6, 2017
  • 1 reply
  • 763 views

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 has been closed for replies.

1 reply

Inspiring
December 6, 2017

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.

Participant
December 6, 2017

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

Thom Parker
Community Expert
Community Expert
December 6, 2017

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.

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