Raccoon
Raccoon

Reputation: 438

How to round value to 2 digits after coma or till first digit greater than 0 in Rails 3

I'd like round

I'm using ruby 1.9.3 and I've tried value.round(2) which gives me only 0.00 and this doesn't satisfy me.

Upvotes: 0

Views: 919

Answers (1)

amine
amine

Reputation: 522

i think number_with_precision is the perfect answer to your question

to have the result you're looking for, you should set the options :precision => 2 and :significant => true

for your given examples, you would use

number_with_precision(0.00000012344, :precision => 2, :significant => true)

number_with_precision(0.0035, :precision => 2, :significant => true)

number_with_precision(76.543554, :precision => 2, :significant => true)

for further precisions see the rails doc http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_with_precision

Upvotes: 3

Related Questions