How can I right pad an integer with zeros? #1153
-
How can I left-align an integer and pad it to the right with zeros? For example, left-aligning 123 and padding it with zeros to its right would produce "123000". Miller function
Can |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
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
I think Miller could benefit from |
Beta Was this translation helpful? Give feedback.
-
hi @derekmahar , probably It's not elegant and it's not what you're looking for. Starting from this input file
and using powers of 10, and cell length, you could run something like
to have
|
Beta Was this translation helpful? Give feedback.
-
I needed to left-pad a string, here's a slight mod to @derekmahar's function:
Some of my unpadded strings could pass as numbers (
|
Beta Was this translation helpful? Give feedback.
-
@derekmahar @aborruso #1205 is in progress |
Beta Was this translation helpful? Give feedback.
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 stringpadding
to input string