Skip to main content
Participant
May 4, 2016
Question

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

  • May 4, 2016
  • 3 replies
  • 3501 views

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?

This topic has been closed for replies.

3 replies

Participant
July 10, 2021

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

Legend
May 4, 2016

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.

try67
Community Expert
Community Expert
May 4, 2016

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