7 Replies Latest reply on Apr 27, 2014 5:42 PM by pademo57

# Adding Leading Zeros to a field which uses the random function

### Post

Hi There,

I've created a field which uses a random function to create a number from 1 to 30. However, I would like the field to show a leading zero if the number created is less than 10, in other words it should contain two characters no matter what.

Currently the field is a number field, do I need to change this to a text field?

If so, could someone tell me exactly what the script should look like please?

• ###### 1. Re: Adding Leading Zeros to a field which uses the random function

Change the calculation to text.   Wrap your Random calculation with Right ("000000000" & RANDOM , 10)

No matter if it is one digit or two digits, it pads 10 zeros to the front, then gets the 10 right most characters.

• ###### 2. Re: Adding Leading Zeros to a field which uses the random function

Thanks TKnTexas,

I hate to be so pedantic, but if I only want 2 characters I would set it up like this:

Right ("00" & RANDOM , 2)

Is that right?

• ###### 3. Re: Adding Leading Zeros to a field which uses the random function

I just tried using the calculation:

Right ("00" & RANDOM , 2)

However, it keeps putting in a number with two decimal places like this:

5.67

And when I go back to look at the calculation it changes it to:

Right ("00" & RANDOM ; 2)

The program changes the comma to a semi-colon.

Is there another way around this?

• ###### 4. Re: Adding Leading Zeros to a field which uses the random function

I've created a field which uses a random function to create a number from 1 to 30

That calculation is what you should put in place of the word RANDOM in the calculation examples shown here. And we are assuming you want integers from 1 to 30.

Right ("00" & int ( RANDOM * 10 ) ; 2)

• ###### 5. Re: Adding Leading Zeros to a field which uses the random function

Note: Haven't messed with random functions in a while, the correct calc may be int ( Random * 10 ) + 1.

• ###### 6. Re: Adding Leading Zeros to a field which uses the random function

Right ("000" & round ( random, 0) ,2)

I always add one more zero to the pad than what I am padding to... just my quirky rule.

• ###### 7. Re: Adding Leading Zeros to a field which uses the random function

Thanks to both PhilModJunk and TKnTexas,

What worked for me was using a compromise between both of your answers:

Right "00" & Round(Int(mycalc ...

Thanks again