Expression for padding zeros

One Star

Expression for padding zeros

Hi,
I am new to Talend, please help. I need help to create an expression in Expression builder with the following logic.
IL10.provNumber is a string value. I need to add zeros to the left of the string based the string value. Finally, the string should have 14 characters length.
If(IL10.provNumber.length ==5) {
IL10.providerNumber = '000000000' + IL10.provNumber ; // If string IL10.provNumber's length is 5 characters, add 9 zeros to make the 14 char string
}else if(IL10.provNumber.length ==4) {
IL10.providerNumber = '0000000000' + IL10.provNumber ; // If string IL10.provNumber's length is 4 characters, add 10 zeros to make the 14 char string
}
Thanks in advance for your help.
Community Manager

Re: Expression for padding zeros

Hi
Try
IL10.provNumber.length ==5?"000000000" + IL10.provNumber:"0000000000" + IL10.provNumber
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Expression for padding zeros

Hi
Try
IL10.provNumber.length ==5?"000000000" + IL10.provNumber:"0000000000" + IL10.provNumber
Best regards
Shong

Hi,
Thank you verymuch for your reply. Yes, it worked. How can i have an expression for the below requirement.
only if the provNumber.length < 6
pad zeros to make total length 14 characters
For Example., if provNumber = abcde, the o/p should be 000000000abcde
Regards,
Jagadish.
Community Manager

Re: Expression for padding zeros

row1.provNumber.length < 6 ?"000000000"+row1.provNumber:row1.provNumber
----------------------------------------------------------
Talend | Data Agility for Modern Business
One Star

Re: Expression for padding zeros

row1.provNumber.length < 6 ?"000000000"+row1.provNumber:row1.provNumber

Hi Shong,
Thank you. But the expression works only if the length is 5.
For example., if row1.provNumber="12345", the output would be 00000000012345. This is fine, got 14 chars length.
row1.provNumber.length < 6 ?"000000000"+row1.provNumber:row1.provNumber

But If the length is 4, that is row1.provNumber="1234", the output would be 0000000001234. This output only 13 characters length. But the expected output is 00000000001234 which is of 14 characters length.
row1.provNumber.length < 6 ?"000000000"+row1.provNumber:row1.provNumber
Similarly for the other cases where the length is 3, 2, 1 and 0. Is this is possible with Expression builder ?

Regards,
Jagadish.
Community Manager

Re: Expression for padding zeros

Hi
Ok, I get you now, you want to pad zero before the number if the length of string less than 14, try this expression:
String.format("%014d", Integer.parseInt(row1.provNumber))
Best regards
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business