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

Share:
18,250
user4965201
Author by

user4965201

Updated on June 17, 2022

Comments

  • user4965201
    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 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

  • user4965201
    user4965201 over 8 years
    i am getting ArgumentError (comparison of String with 0 failed) ... i am using this method in rails
  • Dusht
    Dusht over 8 years
    You can convert your string to float like value.to_f
  • user4965201
    user4965201 over 8 years
    i am getting some value as 0, think thats why i am getting the error
  • Dusht
    Dusht over 8 years
    Can you please provide your code, where you are actually using it.
  • user4965201
    user4965201 over 8 years
    cost = number_with_precision((to_mb(monthly_usage) * cost_per_mb).to_f, :precision => 2)
  • user4965201
    user4965201 over 8 years
    getting this error ArgumentError (comparison of ActiveSupport::SafeBuffer with 0 failed):
  • Dusht
    Dusht over 8 years
    I am assuming, you are getting correct values from to_mb(monthly_usage) and cost_per_mb. Also have you included include ActionView::Helpers::NumberHelper ? Can you please also try with '%.2f' % (to_mb(monthly_usage) * cost_per_mb)
  • user4965201
    user4965201 over 8 years
  • user4965201
    user4965201 over 8 years
    i am getting this error ArgumentError (comparison of String with 0 failed):
  • Gagan Gami
    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
    Gagan Gami over 8 years
    @user4965201 : If you wanted to compare then add .to_f at the end. check
  • user4965201
    user4965201 over 8 years
    i 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
    Dusht over 8 years
    @user4965201 You can do cost = ('%.2f' % (to_mb(monthly_usage) * cost_per_mb)).to_f
  • Gagan Gami
    Gagan Gami over 8 years
    what is value of monthly_usage and cost_per_mb and what is to_mb(monthly_usage)
  • Gagan Gami
    Gagan Gami over 8 years
    do it like ('%.2f' % ((to_mb(monthly_usage) * cost_per_mb)).to_f )
  • Gagan Gami
    Gagan Gami over 8 years
    @user4965201 ('%.2f' % ( 0.02 * 278290).to_f) this will return "5565.80"
  • user4965201
    user4965201 over 8 years
    but i want it as 5565.80 when i do "5565.80".to_f it gives 5565.8
  • Gagan Gami
    Gagan Gami over 8 years
    to_f will remove last zero after . so if it is 1.20 become 1.2 and 1.23 will remain same.