Rounding UP by 0.05

I am almost there but not quite. I need to round UP the computed tax
amount to the nearest 5 cents (or $ 0.05). I have gotten to the point
where I can round (up or down) to the nearest 5 cents. Here is my

computer the tax for each item given quantity, price, and percent tax

def compute_tax(quantity, price, percent_tax)
raw_value = (quantity * price * (percent_tax/100.0))
rounded_value = (raw_value * 20).round / 20.0

This does not work for the cases where the computation of raw_value
works out to something like 0.5625 for example. The function shown
above returns 0.55. I would like it to return 0.60 in such cases.
Cannot figure this out.

Appreciate any help in advance.




On Thu, Sep 9, 2010 at 4:08 PM, Bharat R. [email protected]

rounded_value = (raw_value * 20).round / 20.0

rounded_value = (raw_value * 20).ceil / 20.0


I couldn’t catch you that much, but I tried something like:

(0.5625*10).ceil.to_f/10 # => 0.6

That will round the number always up.

I don’t know if that would help you.

Daniel Gaytán

2010/9/9 Bharat R. [email protected]

rounded_value = (raw_value * 20).ceil / 20.0

works like a charm. Many thanks.


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs