cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Add Leading Zeros

dwhansen-cbg
Contributor

I have strings that are storing a two digit number (i.e. 00, 01, 02, etc) but I need to add another leading zero (i.e. 000, 001, 002). I donโ€™t want to simply concatenate a 0 on the front in case I have values that end up being three digits.

1 ACCEPTED SOLUTION

dwhansen-cbg
Contributor

Thank you @tstack! That worked!

View solution in original post

10 REPLIES 10

christwr
Contributor III

Should be doable with sprintf() string function.

This worked well. I was padding a string with spaces. Beware that when I validated the pipeline the string in the preview data was not displayed with the padding, but the string was actually padded as expected.

dwhansen-cbg
Contributor

$value = โ€œ01โ€.
Iโ€™ve tried $value.sprintf(โ€œ%03dโ€, โ€œ0โ€) and several other similar things but it doesnโ€™t seem to work.

It looks like the format and the value to format are switched around. The sprintf() method is to be called on the format string, like so:

"%03d".sprintf(parseInt($value))

Note that the value passed in needs to be a number and not a string, which is why I used parseInt().