Adding Zeros in Front of a Number to Reach Certain Number of Digits
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Dear all,
I have a very basic question.
Imagine I have a list consisting of many entries with integer values of different number of digits, sometimes 5 sometimes 7. In the end I want to have a string with 10 digits/chars. That means if an entry has 5 digit number then 5 times zero should be added in front of that integer, if it has 7 digit number then 3 times zero.
Obviously, I can do this with a giant if else loop but I would very much appreciate an elegant way that will save me some time!
Best,
Atamert
Solved! Go to Solution.
- Labels:
- Transformation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
You can use the Formula Tool,
padleft([Field1], 10, '0')
You would need to make sure [Field1] is a string by either using the Select Tool or ToString([Field1]).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Any way we can use this for a double data type? Says for string only.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Notify Moderator
Try in the formula tool:
tonumber(padleft(tostring([field1],#),10,"0"))
Might take a bit of experimentation, but this should work. In the example above, the # is the number of decimal places. Not sure if a double data type will store leading zeroes though, so you might not want to convert back to a number.
