Macro Express Forums

# Outputting numbers with leading zeros?

## Recommended Posts

Here's what I'm trying to do: in a loop I want to increment a number by 1, starting with 1, but I'd like it to output with leading zeros.

For the first nine numbers, I'd want:

0000000001

0000000002

0000000003

Once it trips 10, I'd want 8 leading zeros:

0000000010

0000000011

and 100:

0000000100

0000000101

and so on. How can I do this in ME3? Any help is greatly appreciated.

Dan

##### Share on other sites

I think using Excel might be quicker. Just use A1=1, A2=A1+1, A3=A2+1 and copy the formula all the way down the page in column A. Next, highlight the entire column, click Ctrl+1, click the number tab, click custom in the Category section and type 10 zeros in the "Type" box, then copy column A where you need it.

Using ME, the only way I coul think of is copying a formula like "if counting integer greater than 0 and less than 10, add 9 zeros in front of the counting integer, next if counting integer greater than 9 and less than 100, add 8 zeros in front of the counting integer, next if number greater than 99 and less than 1000, add seven numbers in front of the counting integer, and so on....

Alex

##### Share on other sites

I haven't written the code, but something like this should work:

Variable Set Integer N1 = 0

REPEAT UNTIL N1 greater than x (x is however many numbers you want to output)

Variable Modify Integer N1 (add 1 to N1)

Variable Modify Integer | Convert Text to String (N1 into string T1)

Variable Modify String | Pad Left T1 width of 9 (pads T1 with leading spaces, total length of resulting string will be 9 characters)

Variable Modify String | Replace Substring in T1, replace all instances of space with zero

Output string wherever you want it to go

REPEAT END

##### Share on other sites

That's a good one.

##### Share on other sites

rberq's suggestion works great!!!

Thanks!!

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.