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

Pad field with zero

heidi_andrew
Contributor

How can i take a sql result field where I am counting the number of people in the file for a trailer and left pad with zeroes? I have tried a bunch of the expressions shown in several of the answers here but nothing is working. I get
image

1 ACCEPTED SOLUTION

It is because the COUNT is an integer, try with this:

"0000000000".slice($COUNT.toString().length) + $COUNT

View solution in original post

10 REPLIES 10

That worked perfectly! You rock!

koryknick
Employee
Employee

@heidi.andrew - one other option - a while back, I created a set of library expression files, one of which contains LPAD and RPAD functions. Basically, this simplifies this type of expression and prevents multiple solutions to the problem from different developers.

I hope this helps!

heidi_andrew
Contributor

i have been in that one a lot but nothing works for me. I tried several iterations of this:
lib.string.lpad(โ€˜123โ€™, 5, โ€˜0โ€™) yields โ€œ00123โ€

i brought it into a mapper field. Is that the right snap? I changed the " from the curved version and still get the error like this:

image

koryknick
Employee
Employee

Can you share your pipeline? If you have the library referenced in your pipeline then the statement you show should work.

ddellsperger
Employee
Employee

If you have a guaranteed numeric value (integer) you can simply use "%010d".sprintf($COUNT) to get a string out, you can do similar with float values, but you have to be a bit more tricky about it. I believe it follows the Java util formatter specification.