Site Tools


Padding numbers with leading zeros

Developer: RhinoScript
Summary: Demonstrate how to pad numbers with leading zeros.

Question

How do I pad digits with leading zeros? For example, if I have the number 24, how can I print it as “0024”?

Answer

The following utility function will pad an integer with a specified number of digits.

Function PadDigits(val, digits)
  PadDigits = Right(String(digits,"0") & val, digits) 
End Function 

You can use this function as such:

For i = 0 To 25
  Rhino.Print PadDigits(i, 4)
Next

The output, printed to the command, will be:

0000
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
...


developer/scriptsamples/paddigits.txt · Last modified: 2010/11/05 by dale