Skip to content

How can I right pad an integer with zeros? #1153

Answered by derekmahar
derekmahar asked this question in Q&A
Discussion options

You must be logged in to vote

Turns out that I required a function that left-aligns and right-pads a string, not an integer. I needed this specifically in order to left-align and right-pad fractional seconds of an ISO 8601 timestamp string. I couldn't use an integer to represent the decimal digits of the fractional seconds because fractions such as "085" or "001" are technically not integers and even if Miller could convert them to integers, it would strip the significant leading zeros. Instead, I had to treat these fractional seconds as strings that preserved all digits, including leading zeros.

To achieve this goal, I wrote the following function right_pad that appends repeated copies of string padding to input string

Replies: 4 comments 7 replies

Comment options

You must be logged in to vote
1 reply
@johnkerl
Comment options

Answer selected by derekmahar
Comment options

You must be logged in to vote
3 replies
@derekmahar
Comment options

@derekmahar
Comment options

@aborruso
Comment options

Comment options

You must be logged in to vote
3 replies
@derekmahar
Comment options

@amp42
Comment options

@derekmahar
Comment options

Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants