Working out sales tax

Hi,

Having googled extensively about storing money in Rails, it seems to be
accepted that the best way is to store the prices etc in the smallest
part of the currency so that you can use an integer i.e £20 would be
stored as 2000 pence.

This is fine and I’ve made a nice pretty wrapper that outputs the price
in the standard form for invoicing and what not - but here I’ve come
across a problem. My company is registered for VAT (Sales Tax) which I
have to charge at 17.5%.

How would I go about multiplying the original cost in pence but a
floating point? According to the the tax rules, if the final result is
over half way it should be rounded up, otherwise it should be rounded
down i.e

1.743 should round down to 1.74
1.16 should round up to 1.17

I’m not sure the best way to do the multiplication so that I end up with
an integer that follows the rules. This is pretty much the only thing
holding back which in all other ways is a great little system.

Any help would be very very gratefully received!

Cheers,

Pete

Hi,

Sorry to repost but would this solution be ‘safe’ ?

cost_including_tax = (cost * 1.175).to_i

Or would it still potentially suffer from rounding problems as it still
uses a floating point number?

Cheers,

Pete

Try using Float.round ?
http://www.rubycentral.com/book/ref_c_float.html#Float.round

-Pratik

P.S -> I hope you’ve seen the movie “Office space”

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