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

How to create a format that involves leading zeros after a calulation

Participant ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

I have a serial number log form (PDF) where the first serial number entered will be 0001 (it shows the leading zeros in the first text box. Formatting set to NONE ) and there after increment the following text box by 1. The calculation to increment by 1 is not the issue. I cant seem to format the text box to show the leading zeros. The second serial number comes out as "2" not "0002" as I would liked.

I take it that I'm going to need some type of "Format" java script here to get the leading Zeros to show up?

TOPICS
Acrobat SDK and JavaScript , Windows

Views

2.2K

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 04, 2016 May 04, 2016

Copy link to clipboard

Copied

Can you post the code you're using? A small adjustment to it should be enough.

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
Advocate ,
May 04, 2016 May 04, 2016

Copy link to clipboard

Copied

The calculation does turn a string containing numbers into a number. That's why the leading zeroes disappear.

To get them back, we can use a Custom format, using the util.printf() method.

In the case here, you would add the following to the Format event of the field:

event.value = util.printf("%04d", event.value) ;

and that should do it; the number (4) stands for the number of digits in total.

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 ,
Jul 10, 2021 Jul 10, 2021

Copy link to clipboard

Copied

LATEST

Johnny, your solution is the most simplest and elegant solution. Thank you. I was messing around for hours with If/else if.etc. Finding out that JS can't get the length of a number unless it's a string (was trying to use LEN/.length to determine if single digit), I saw all these other solutions and my gosh, yours? ONE LINE! AWESOME! Thanks again

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