Skip to content

Latest commit

 

History

History
22 lines (17 loc) · 357 Bytes

zero-padding.md

File metadata and controls

22 lines (17 loc) · 357 Bytes

Zero Padding

Ruby makes zero-padding strings to a fixed length easy with String#rjust.

> "1234".rjust(6, "0")
=> "001234"
> "123456".rjust(6, "0")
=> "123456"

In the same way, you can pad zeros on the other side of the string with String#ljust.

> "12".ljust(4, "0")
=> "1200"
> "".ljust(4, "0")
=> "0000"

h/t Dillon Hafer