How do I rounding of zeros in Ruby

Viewed 165

When I round of 12121.232323 to 2 digit decimal point like below

p 12121.232323.round(2)

it is printing

12121.23

But when I try to round()

211211.00000.round(2)

it's printing

211211.0

But I want

211211.00

How do I do that?

2 Answers
'%.2f' % 12121.232323

or

include ActionView::Helpers::NumberHelper

number_with_precision(value, :precision => 2) 

What you seek is not exactly rounding but formatting.

You can select your float formatting like this:

p "%.2f" % 12121.0000

where the %.2f part means "show 2 decimal points"

Related