Djoezz said:
Hi all,
Is there any string build-in function that convert numeric to string
according to the digit we specify. So that 1 can be converted to "01"
(for 2 chars) or "001" (for 3 chars)?
Thanks in advance,
Djoezz
You could use the Format function:
?Format(1, "0")
1
?Format(1, "00")
01
?Format(1, "000")
001
If you need to be able to specify the number of digits as an argument,
instead of supplying a literal like "000", you might use the Format
function in conjunction with the String function:
numdigits = 1 : ?Format(1, String(numdigits, "0"))
1
numdigits = 2 : ?Format(1, String(numdigits, "0"))
01
numdigits = 3 : ?Format(1, String(numdigits, "0"))
001
Note that, if the number of digits required to represent the original
number exceed the number of digits you specify, all of the above
expressions will return a value longer than the number of digits
specified. For example,
numdigits = 3 : ?Format(12345, String(numdigits, "0"))
12345
If that's a problem, you could solve it by using either the Left or
Right function to truncate the result on one side or the other:
ndig = 3 : ?Right(Format(12345, String(ndig, "0")), ndig)
345
ndig = 3 : ?Left(Format(12345, String(ndig, "0")), ndig)
123