Why You Shouldn’t Use Float for Currency (floating point issues – explained for Ruby and RoR)

This article is a response to https://vladzloteanu.wordpress.com/2010/01/06/ruby-on-rails-interview-questions-update/

It is a VERY BAD IDEEA to use floating point arithmetics to deal with currency. In most of the programming languages. Basically, because you’ll end up loosing money :). And this (on the great majority of cases) is not desirable 🙂 .

I’ll show you some magic (that you may try at home):
~$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
~$ irb
irb(main):003:0> (10.12*100).to_i
=>; 1011

So, what just happened? Continue reading

Ruby on Rails Interview questions – update

Most of Google searches that hit my blog are about ‘ruby on rails interview questions‘.

Responding to this popular request 😉 , I will post 3 more questions that are related.

1. What type of data would you use to represent currency?

2. Why is it not a good practice to use float for currency?

3. Explain the following behaviour:

~$ ruby --version
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
~$ irb
irb(main):003:0> (10.12*100).to_i
=>; 1011

The answer – in my next post .

Database UML tool for MySQL – automatic UML schema from DB

MySQL has a UML tool that also can reverse engineer the existing tables.
It is free (of course 🙂 ) and you can grab it from here: http://dev.mysql.com/workbench/

Java Lists: Group by element property (as Ruby group_by method)

This is a very funny function, similar to group_by from Ruby.
It takes a collection and it transforms to a hash, having keys the property *groupBy*, and values the objects having that property.

A usage example:

collectionToHash(users, "name"); // returns a hash where the keys are the distinct user names, and the values are lists of users having that property.

Continue reading