ruby round off to two decimal place and keep zero
18,250
Solution 1
This will do the trick:
> sprintf("%.2f",(0.02 * 270187))
#=> "5403.74"
> sprintf("%.2f",(0.02 * 278290))
#=> "5565.80"
> sprintf("%.2f",(0.02 * 270187)).to_f > 100 # If you plan to Compare something with result
#=> true
OR
> '%.2f' % (0.02 * 270187)
#=> "5403.74"
> '%.2f' % (0.02 * 278290)
#=> "5565.80"
Demo
Note: The result is always a string, but since you're rounding I assume you're doing it for presentation purposes anyway. sprintf can format any number almost any way you like. If you are planning to compare anything with this result then convert this string to float by adding .to_f
at the end. Like this
Solution 2
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
Author by
user4965201
Updated on June 17, 2022Comments
-
user4965201 almost 2 years
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 correctbut
(0.02 * 278290).round(2)
is 5565.8 which is not consistent with previous onei want to make it look like 5565.80
Please tell me how can i do it in ruby
-
user4965201 over 8 yearsi am getting ArgumentError (comparison of String with 0 failed) ... i am using this method in rails
-
Dusht over 8 yearsYou can convert your string to float like
value.to_f
-
user4965201 over 8 yearsi am getting some value as 0, think thats why i am getting the error
-
Dusht over 8 yearsCan you please provide your code, where you are actually using it.
-
user4965201 over 8 yearscost = number_with_precision((to_mb(monthly_usage) * cost_per_mb).to_f, :precision => 2)
-
user4965201 over 8 yearsgetting this error ArgumentError (comparison of ActiveSupport::SafeBuffer with 0 failed):
-
Dusht over 8 yearsI am assuming, you are getting correct values from
to_mb(monthly_usage)
andcost_per_mb
. Also have you includedinclude ActionView::Helpers::NumberHelper
? Can you please also try with'%.2f' % (to_mb(monthly_usage) * cost_per_mb)
-
user4965201 over 8 yearsLet us continue this discussion in chat.
-
user4965201 over 8 yearsi am getting this error ArgumentError (comparison of String with 0 failed):
-
Gagan Gami over 8 years@user4965201 : as I already said it will return as string. and you are trying to compare something with this result further
-
Gagan Gami over 8 years@user4965201 : If you wanted to compare then add
.to_f
at the end. check -
user4965201 over 8 yearsi have done this cost = ('%.2f' % (to_mb(monthly_usage) * cost_per_mb)).to_f but i got a record like 5565.8 , no zero at the end
-
Dusht over 8 years@user4965201 You can do
cost = ('%.2f' % (to_mb(monthly_usage) * cost_per_mb)).to_f
-
Gagan Gami over 8 yearswhat is value of
monthly_usage
andcost_per_mb
and what isto_mb(monthly_usage)
-
Gagan Gami over 8 yearsdo it like
('%.2f' % ((to_mb(monthly_usage) * cost_per_mb)).to_f )
-
Gagan Gami over 8 years@user4965201
('%.2f' % ( 0.02 * 278290).to_f)
this will return"5565.80"
-
user4965201 over 8 yearsbut i want it as 5565.80 when i do "5565.80".to_f it gives 5565.8
-
Gagan Gami over 8 years
to_f
will remove last zero after.
so if it is1.20
become1.2
and1.23
will remain same.