• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Remove Trailing Zeros in a Decimal

New Here ,
Mar 24, 2020 Mar 24, 2020

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,

TOPICS
PDF forms

Views

8.8K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 24, 2020 Mar 24, 2020

Custom format script

 

event.value = util.printf("%,02.2f",event.value).replace(/\.?0+$/,"");

Votes

Translate

Translate
Community Expert ,
Mar 24, 2020 Mar 24, 2020

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 24, 2020 Mar 24, 2020

Copy link to clipboard

Copied

Custom format script

 

event.value = util.printf("%,02.2f",event.value).replace(/\.?0+$/,"");

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 24, 2020 Mar 24, 2020

Copy link to clipboard

Copied

Beautiful! That did it. Thanks Thom!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 30, 2021 May 30, 2021

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Change the last bit of the code Thom provided to:

 

replace(/\.?0{2,}$/,"");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2021 May 30, 2021

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"...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2021 May 30, 2021

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2021 May 30, 2021

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.

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

He wants to leave if it's 00 not remove it.

EDIT: actually nvm i got it wrong.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

No, they want to remove it:

 

quote9.00 becomes 9



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 30, 2021 May 30, 2021

Copy link to clipboard

Copied

Yea, i thought he wants opposite, realise it after already posted 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2021 May 31, 2021

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2021 May 31, 2021

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);

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 31, 2021 May 31, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 31, 2021 May 31, 2021

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.  

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 01, 2021 Jun 01, 2021

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  ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

What do you mean by "Exports the Value"?

 

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 01, 2021 Jun 01, 2021

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

The use a script to round its value.

For example:

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2021 Jun 02, 2021

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2021 Jun 02, 2021

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2021 Jun 02, 2021

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Just want to post an example of eventual errors.

As you can see the final colomn should show 9.

But because 8.50 in the previous colomn is not really 8.50, this error occurs ( it shows 8 instead of 9 ) .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 02, 2021 Jun 02, 2021

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);

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines