Hi everybody,
I'm trying to make a form using Adobe Acrobat.
I want a field that usually has only 2 decimal positions, to have 4 decimal positions ONLY when needed.
Example: if field value is 3,53 it's fine, but if value is 3,5346 I want it to show it all.
I've played around field properties and I've realized that I need to use a custom format script. I don't know where to start, though. Can you point me in the right direction?
Thank you in advance.
Currency is Euro ( € ). Thousands separator is the point ( . ) and decimal separator is the comma ( , )
Example: € 1.234,56
Even a simple direction will do. I'm not new to JS but I never used it into PDFs, as I mainly use it in webpages to manipulate the DOM and such.
I'm sure the solution is simpler than I think, but it's just confusing me at the moment ![]()
I just tossed the following together quickly and didn't do much testing, to take is as general guidance. It is intended to be placed in a document-level JavaScript and called from the field's custom Format script. You can't simply place it all as the Format script due to the way Acrobat behaves when you do.
The point of the script is to calculate the number to use as the first parameter to the AFNumberFormant function, which is what Acrobat uses when you set up a numeric format. I couldn't think of a clever name for the function, so I'll leave that to you:
function fmt1() {
// Set the minimum number of digits to the right of the decimal
var min = 2;
// Get the field value, as a string
var val = event.value;
// Round to the nearest 10-thousand of a cent
var rn = util.printf("%.4f", val);
// Replace any trailing zeroes with nothing
rn = rn.replace(/[0]+$/, "");
// Get the number of characters after the decimal
var num = rn.split(".")[1].length;
// Set to the minimum if appropriate
if (num < 2) num = 2;
// Use built-in formatting function
AFNumber_Format(num, 2, 0, 0, "\u20ac", true);
}
Call it like this in the field's Format event:
fmt1();
If this is not a calculated field, but one that a user will interact with, you'll want to add a corresponding Keystroke function, something like:
function keystroke1() {
AFNumber_Keystroke(2, 2, 0, 0, "\u20ac", true);
}
Works like a charm! ![]()
I just replaced this:
// Set to the minimum if appropriate
if (num < 2) num = 2;
with this:
// Set to the minimum if appropriate
if (num < min) num = min;
because I thought it's what you meant.
Thank you so much, I can't tell you how grateful I am.
Should you ever come to Italy, I owe you a beer ![]()
North America
Europe, Middle East and Africa
Asia Pacific