ruby - How do I round to two decimal places? -
i want items rounded such this:
(5.101 * 100).round / 100.0
to output this:
5.10
instead of this:
5.1
how do in ruby?
there couple ways, favor using string's %
(format) operator:
'%.2f' % [(5.101 * 100).round / 100.0] # => "5.10"
kernel's sprintf
method has documentation various flags , modifiers. there's kernel's printf
but, said, i'd go %
.
Comments
Post a Comment