Copy link to clipboard
Copied
I have Number fields in an Adobe Acrobat form that need to be able to be entered as either a whole number or a decimal. Easy enough. I also need up to two decimal places shown, also easy. However, I can't figure out a way to hide the trailing zeros in a decimal in order to "clean up" the look of it. For example:
I type What appears I want
8.25 8.25 8.25
22.7 22.70 22.7
3 3.00 3
Does anyone know how to do this?
Thanks,
Copy link to clipboard
Copied
Custom format script
event.value = util.printf("%,02.2f",event.value).replace(/\.?0+$/,"");
Copy link to clipboard
Copied
You can't do that with the built-in Number format setting. You'll need to write your own Format script.
Copy link to clipboard
Copied
Custom format script
event.value = util.printf("%,02.2f",event.value).replace(/\.?0+$/,"");
Copy link to clipboard
Copied
Beautiful! That did it. Thanks Thom!
Copy link to clipboard
Copied
Thanks. Exactly what I needed.
Is there any way to remove just the trailing double zeros and not all of them ?
For example
9.57 remains 9.57
9.50 remains 9.50
9.00 becomes 9
Thanks
Copy link to clipboard
Copied
Change the last bit of the code Thom provided to:
replace(/\.?0{2,}$/,"");
Copy link to clipboard
Copied
PS. I think there's a mistake in that code. I would remove the question mark after the dot. If you don't it will also convert "2500" to "25"...
Copy link to clipboard
Copied
Although the util method adds the period with the two decimals after it, so I guess that won't happen, but I would still remove it...
Copy link to clipboard
Copied
Yes, the replacement is controlled with a regular expression, so it's just a matter of writing the RegExp to match 2 trailing 0's.
https://www.pdfscripting.com/public/Pattern-Matching-with-Regular-Expressions.cfm
event.value = util.printf("%,02.2f",event.value).replace(/\.00\s*$/,"");
This regular expression exactly matches ".00" at the end of the number.
Copy link to clipboard
Copied
He wants to leave if it's 00 not remove it.
EDIT: actually nvm i got it wrong.
Copy link to clipboard
Copied
No, they want to remove it:
9.00 becomes 9
Copy link to clipboard
Copied
Yea, i thought he wants opposite, realise it after already posted 🙂
Copy link to clipboard
Copied
Thanks. Worked perfectly.
Can you please help me with another problem .
I have a list of values ( on the left colomn of the picture with decimals) which I would like to be rounded up in the second colomn without decimals. They seem to work but the third line ( 7.50 ) should become 8, but it doesn't. It goes to 7. In truth the value of 7.50 is actually 7.497778. That is what's causing the problem. Is there a solution to this.
Thank you.
Copy link to clipboard
Copied
One way to do this would be to add .005 to the number before rounding to catch the numbers on the edge.
This could be used in the calculation script for the second column fields.
event.value = Math.round(Number(this.getField('Column1.row1').value) + 0.005);
Or, since column 2 is dependent on a single value, Use the validate event on the column 1 field with this script.
this.getField("Column2.row1").value = Math.round(Number(event.value) + 0.005);
Copy link to clipboard
Copied
Thanks. Your solution works fine but is there another way.
The value of 7.50 is the result of a previous calculation of the average of 3 fields.
When I click on it, I can see that the actual (export) value is 7.497778.
Is there a way so that the average of the 3 fields is 7.50 and that 7.50 will also be the export value.
Copy link to clipboard
Copied
Just add in the .005 value in the average calculation. You could even put a conditional on it. Anything within .005 of .5 is bumped up.
Copy link to clipboard
Copied
Thank you. Any chance of creating a script that exports the value, considering just the first 2 visible decimals, and not considering all the invisible ones ?
Copy link to clipboard
Copied
What do you mean by "Exports the Value"?
Copy link to clipboard
Copied
Even though the field shows 7,50 , it is really exporting 7,497778 to another field. I need it to show and export 7,50.
Copy link to clipboard
Copied
The use a script to round its value.
For example:
event.value = event.value.toFixed(2);
Copy link to clipboard
Copied
It still seems to be exporting all the decimals.
Any chance of a custom calculation script ?
I'll try to be more specific. I have 3 fields ( "Text1", "Text2", "Text3"). I need the average of these to appear in another field called "Text4", but rounded to 2 decimals.
The problem is that "Text4" along with "Text5" and "Text6", then create other 3 fields where the average will appear in "Text7"and always rounded to 2 decimals. Unfortunately all the exported invisible decimals are messing up the calculations.
I hope that is clear.
Thank you in advance for any advice you can provide.
Copy link to clipboard
Copied
Did you implement the code I provided? If so, where did you place it? Can you share the actual file with us?
Copy link to clipboard
Copied
Thanks for your time ( and patience ).
I have attached a demo of what I'm trying do as I'm getting a bit confused so I decided to erase some scripts that you have suggested so we can start from the beginning.
As you can see:
The average of the colomns 1, 2 and 3 is the value of colomn A. ( which shows 8.62 but if you click on it, it shows 8.623333. That's what I don't want. The export value for the next step has to be 8.62 )
Then the average of the colomns A, B and C is the value of colomn named "A+B+C/3". ( which shows 8.49 but if you click on it, it shows 8.49444. Same problem.
The result of the "final rounded" colomn is the rounded value of the previous "A+B+C/3" colomn.
P.S. the values in the "C" and "final rounded" colomns do not have decimals.
I hope it's clear. Of course most of the times the final result is correct but there are some cases in which all the invisible decimals will influence the final result.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You have not used a calculation script, despite multiple advice to do so.
For example, the script for Text7 should be:
var fields = ["Text4", "Text5", "Text6"];
var total = 0;
for (var i in fields) {
total+=Number(this.getField(fields[i]).valueAsString);
}
event.value = (total/3).toFixed(2);