Rails precision error
Posted
by
dontangg
on Stack Overflow
See other posts from Stack Overflow
or by dontangg
Published on 2011-01-04T07:26:18Z
Indexed on
2011/01/04
7:53 UTC
Read the original article
Hit count: 209
ruby-on-rails
|precision
When I run this in my Rails application:
my_envelope.transactions.sum(:amount)
This SQL is shown in the log files:
SQL (0.3ms) SELECT SUM("transactions"."amount") AS sum_id FROM "transactions" WHERE (envelope_id = 834498537)
And this value is returned:
<BigDecimal:1011be570,'0.2515999999 9999997E2',27(27)>
As you can see, the value is 25.159999. It should be 25.16. When I run the same SQL on the database myself, the correct value is returned.
I'm a little confused because I know that there are precision problems with Floats, but it is returning a BigDecimal. The SQL column type is decimal. Any ideas?
© Stack Overflow or respective owner