×

ruby · level 2 · unit 3 · lesson 2

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

fills a pattern with values, neatly.

Money needs two decimal places, but Ruby shows two pounds fifty as 2.5. will round a float to two places, and .to_f converts a string like "3.75" into a float.

For full control, use format. Give it a pattern with placeholders, then the values to drop in, in order. %s takes any value as text, %d a whole number, and %.2f a float shown with exactly two decimal places.

third = 10.0 / 3
puts third
puts third.round(2)
puts "3.75".to_f + 1
puts format("%s owes £%.2f", "Sam", 2.5)
puts format("%d items", 3)

.round(2) gives a number, but format gives a string, so use format when you show money.