How do I specify a number format with 2 decimal places for a field that already is using a custom format script?

I have Acrobat Pro XI.

How do I specify a number format with 2 decimal places for a field that already is using a custom format script?

I have a form that has many sub-total and total fields. I want those fields to display as empty if the value is zero, so I have entered the following custom format script for the field:

if(event.value == 0) event.value = "";


(slight tangent-- I'd rather have it display as blank, not when the value is zero, but when the fields it is totaling are all blank. I used this script because it was easier, but wouldn't mind advice on that as well).

However, when the summed value is a whole number, say 5, it displays as "5" but the business requirement is for it to display as "5.00" If the sum total is a fraction, like 5.25, it displays this as "5.25", which is my desired format.

I cannot just change the field to a number format with two decimal places because I am already using a custom format script. So, what code do I need to add to the custom format script so that the value always includes 2 decimal places?


S E


2 Answers

Voted Best Answer

If you are already using a custom format script, you cannot use one of the predefined formats at the same time. You will have to either re-implement the formatting in JavaScript, or "borrow" the scripts that Acrobat is using. Take a look here for more information about the latter:

http://khkonsulting.com/2015/09/apply-standard-pdf-form-field-formattingkeystrokevalidation-events-to-fields-via-javascript/

To do it manually, you can use the Number.toFixed() method:

Number(5).toFixed(2);

This will result in 5.00 - instead of the number, you would use the event.value:

Number(event.value).toFixed(2);

This would be done in the "else" branch of your "if" statement.

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


By Karl Heinz Kremer   

Formatting only affects the displayed value so you are not modifying the actual value of the field. To change the value of the field, you need to use the custom calculation script or the validation script.


George Kaiser   


Please specify a reason: