Comments on: 7 Top Tips for Coding With Currency https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency Hot ideas for the web. Sat, 07 Nov 2009 12:39:49 +0000 hourly 1 https://wordpress.org/?v=5.5.6 By: Gawin https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-1250 Sat, 07 Nov 2009 12:39:49 +0000 http://www.setfiremedia.com/blog/?p=101#comment-1250 The Money gem now lives at:
http://money.rubyforge.org/

Code on github:
http://github.com/FooBarWidget/money/

gem install money

]]>
By: Paul Mckay https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-1248 Fri, 06 Nov 2009 12:10:39 +0000 http://www.setfiremedia.com/blog/?p=101#comment-1248 Hey John,

Remembered seeing this article a while back and had some recent experience with dealing with currency.

Just a quick tip for everyone, I agree totally that using floats is terrible for currency, and working with minor units as John suggest is a much better way.

Lest this bites anyone in the ass part way through a design, for e-commerce projects, don’t use cents/pennies as your minor unit. The problem with that is VAT/tax calculations. You often want to store the price ex vat against a product and then calculate the VAT afterwards. The problem with storing your ex vat price in cents/pennies is that it’s impossible (due to rounding) to get certain inc vat prices.

For this reason (according to our accountant) accounting packages store prices ex vat to 100ths of a cent/penny. So you are much better off making that your minor unit.

(Bear in mind, companies that sell a lot internationally will charge a different VAT/tax rate according to the country they are shipping to, in these situations storing price inc vat doesn’t work out).

]]>
By: gauda.de » Blog Archive » Use to_money to convert a string to an integer https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-916 Mon, 03 Nov 2008 00:21:06 +0000 http://www.setfiremedia.com/blog/?p=101#comment-916 […] You can read about the motivation why to use integer in flavor of decimals in the database here: 7 Top Tips for Coding With Currency. […]

]]>
By: sambeau https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-681 Mon, 13 Oct 2008 20:54:46 +0000 http://www.setfiremedia.com/blog/?p=101#comment-681 Just one comment: Floats are not slower than integers any more.

]]>
By: Dev Blog AF83 » Blog Archive » Veille technologique : Merb, Vim, geode, iPhone, etc. https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-678 Mon, 13 Oct 2008 17:41:25 +0000 http://www.setfiremedia.com/blog/?p=101#comment-678 […] : un plugin jQuery pour choisir l’heure en 3 clicks. * http://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency : 7 astuces pour développeurs confrontés à différentes monnaies. * […]

]]>
By: links for 2008-10-08 | Libin Pan https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-545 Thu, 09 Oct 2008 05:49:48 +0000 http://www.setfiremedia.com/blog/?p=101#comment-545 […] 7 Top Tips for Coding With Currency | The Matchbox (tags: rubyonrails ruby rails validation money) […]

]]>
By: links for 2008-10-08 « Donghai Ma https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-542 Thu, 09 Oct 2008 03:51:31 +0000 http://www.setfiremedia.com/blog/?p=101#comment-542 […] 7 Top Tips for Coding With Currency | The Matchbox (tags: tips programming business money ecommerce) […]

]]>
By: Matt https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-538 Thu, 09 Oct 2008 00:52:14 +0000 http://www.setfiremedia.com/blog/?p=101#comment-538 Use a decimal and be done with it.

]]>
By: Currency https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-526 Wed, 08 Oct 2008 18:27:42 +0000 http://www.setfiremedia.com/blog/?p=101#comment-526 […] Main has 7 top tips for coding with currency – with Rails specifically in […]

]]>
By: John Main https://www.setfiremedia.com/blog/7-top-tips-for-coding-with-currency/comment-page-1#comment-514 Wed, 08 Oct 2008 08:22:04 +0000 http://www.setfiremedia.com/blog/?p=101#comment-514 Thanks for commenting guys

@anon – As I’ve said I think the only way to go is integer arithmetic and I do think that if you work in thousandths or ten-thousandths of a penny/cent/whatever then that’s the base unit you should use. As ruertar says, with 64bit ints that still allows you to represent pentillions of your major unit.
And I also agree with Pavel – that’s rather worrying…

@Raoul – If you find one that always gets its sums right and doesn’t clog the CPU with excessive floating point arithmetic and/or casting to and from strings then please send me a link :)

@Me – Of course you have to follow your spec and not make any arbitrary decisions about rounding. I work a lot with percentage discounts, markup etc and believe me, always rounding in one direction makes my life so much easier!

@Steve – To me that is treating the symptoms rather than the disease. If the rest of the system works perfectly well in minor units then increasing your accuracy to handle a single bug is unnecessary complication, especially when there are easier ways round it.
In this example we would be increasing the accuracy but then always wanting the additional least significant digits to be zero as any other value would represent the floating point error.

@David – I work a lot with percentage discounts and, being the nice guys we are, we round them up to the nearest penny. Hence the ‘ceil’.

@Jeremy – Nice one, I’m off to check out that money class right now

]]>