Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby round off to two decimal place and keep zero [duplicate]

i want to round off a number upto two decimal place in ruby such that

(0.02 * 270187).round(2) is 5403.74 which is correct

but

(0.02 * 278290).round(2) is 5565.8 which is not consistent with previous one

i want to make it look like 5565.80

Please tell me how can i do it in ruby

like image 869
user4965201 Avatar asked Nov 19 '15 11:11

user4965201


1 Answers

You could do something like

include ActionView::Helpers::NumberHelper

number_with_precision(value, :precision => 2) # value.to_f if you have string

or like this

'%.2f' % your_value

Hope it helps! Further you can read from here

like image 125
Dusht Avatar answered Sep 26 '22 05:09

Dusht