Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

printf

  • printf
# Padding and Alignment
number = 42
printf "'%-4s'\n", number # Padding, left align
printf "'%4s'\n", number  # Padding, right align
printf "'%04s'\n", number # Padding with 0s

# Rounding floating point
puts Math::PI
printf "'%d'\n", Math::PI
printf "'%f'\n", Math::PI
printf "'%.3f'\n", Math::PI
'42  '
'  42'
'0042'
3.141592653589793
'3'
'3.141593'
'3.142'