Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Streamlining user types in Qlik Cloud capacity-based subscriptions: Read the Details
cancel
Showing results for 
Search instead for 
Did you mean: 
Anonymous
Not applicable

Add leading zero's

I have a text file containing a column "ID". Please see example below. The ID should be an integer with length 3. Now the problem is that some records have values which are two digits long. Is it possible to (in case the length is not three) add leading zero's, just to make it length(3)? I know this is possible with a custom java code (component), however I would like to fix this with a regular expression replace. Anyone some suggestions?
"ID","TITLE"
"123","Title 1"
"456","Title 2"
"78","Title 3"
Labels (3)
5 Replies
Anonymous
Not applicable
Author

Hi
If you want to have a value like "078", you have to change this column as String.
Regards,
Pedro
Anonymous
Not applicable
Author

Hi
If you want to have a value like "078", you have to change this column as String.
Regards,
Pedro

Thanks; forgot to mention, the column IS a string.
Anonymous
Not applicable
Author

I use :
String.format("%3s", <your field> ).replaceAll(" ", "0")
Anonymous
Not applicable
Author

I use :
String.format("%3s", <your field> ).replaceAll(" ", "0")

Thanks! Little improvement to your code:
String.format("%03d", Int.parseInt(row7.ID));
This will add "0"'s to your integer, with a maximum of 3 digits long.
KrishnaApurva
Contributor
Contributor

Hi,

I also have the same doubt and try to get the desired output but get an error (Int cannot be resolved). So, I tried this:

String.format("%3d", Integer.parseInt(<your field>))

and it worked out successfully.