Skip to content
Justice Tech School

Formatting Numbers

round, floor, ceil and to_s with padding format numbers. Use a helper for thousands separators.

Example
n = 1234567.891
puts n.round(2), n.floor, n.ceil, 5.to_s.rjust(3, "0")
puts n.to_i.to_s.reverse.scan(/\d{1,3}/).join(",").reverse

Result

1234567.89
1234567
1234568
005
1,234,567

Exercise

Pad with zeros.

"5".(3, "0")

Quiz Test

What does rjust do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.