# BigDecimal problem

**URL:** <https://www.ruby-forum.com/t/bigdecimal-problem/118695>\
**Category:** Rails\
**Created:** [October 30, 2007, 4:50am UTC](https://www.ruby-forum.com/t/bigdecimal-problem/118695 "2007-10-30T04:50:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmitryame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dmitryame](https://www.ruby-forum.com/u/dmitryame)\
**Post date:** [October 30, 2007, 4:50am UTC](https://www.ruby-forum.com/t/bigdecimal-problem/118695/1 "2007-10-30T04:50:06Z")

</div>

BigDecimal still does not behave 100% correctly,

My relevant model fragment looks like this:

```
  t.column :lat, :decimal, :precision => 20, :scale => 15, :null =>

```

false  
t.column :lng, :decimal, :precision =\> 20, :scale =\> 15, :null =\>  
false

The controller calls a finder

find\_by\_lat\_and\_lng(@lat, @lng) which produces a log line:

SELECT \* FROM geo\_locations WHERE (geo\_locations.`lng` =  
‘-70.85366249084473’ AND geo\_locations.`lat` = ‘42.66684871528651’)  
LIMIT 1

This select returns nothing if ran manually but there is a record in the  
table corresponding to the lng and lat criteria.  
If I remove quites around lat and lng values then it works and returns  
one record (as it should be), like this:  
SELECT \* FROM geo\_locations WHERE (geo\_locations.`lng` =  
-70.85366249084473 AND geo\_locations.`lat` = 42.66684871528651) LIMIT 1

Is there a way to tell rails to send decimal values without quites (I’m  
assuming that quoted values are treated as strings and that’s why they  
don’t match).

Any ideas?

---

<div class="post-metadata">

**Author:** ![dmitryame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dmitryame](https://www.ruby-forum.com/u/dmitryame)\
**Post date:** [October 30, 2007, 2:07pm UTC](https://www.ruby-forum.com/t/bigdecimal-problem/118695/2 "2007-10-30T14:07:26Z")

</div>

When the finder parameters converted to float like this  
find\_by\_lat\_and\_lng(@lat.to\_f, @lng.to\_f) then it does send the  
parameters into the query non quoted. The problem is that the float does  
not have enough precision (at least not in my case) and the finder still  
does not return the value. When the decimal values are used in the  
finder like this find\_by\_lat\_and\_lng(BigDecimal.new(@lat),  
BigDecimal.new(@lng)) the query does quote the parameters.

So, my question is: shouldn’t BigDecimal behave similar to Float in this  
regard, after all they both numbers. Seems like a bug to me.

If anyone has any advise how to deal with this situation, would be  
highly appreciated.

thanks

---

<div class="post-metadata">

**Author:** ![dmitryame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dmitryame](https://www.ruby-forum.com/u/dmitryame)\
**Post date:** [October 30, 2007, 10:16pm UTC](https://www.ruby-forum.com/t/bigdecimal-problem/118695/3 "2007-10-30T22:16:40Z")

</div>

The question is if LongDecimal (gem-name: long-decimal) would not be the  
better match for database fields than  
BigDecimal, which is more or less a Float with the capability to have  
more digits.

Best regards,

Karl

---

<div class="post-metadata">

**Author:** ![dmitryame](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/48db29/32.png) [@dmitryame](https://www.ruby-forum.com/u/dmitryame)\
**Post date:** [October 31, 2007, 12:00am UTC](https://www.ruby-forum.com/t/bigdecimal-problem/118695/4 "2007-10-31T00:00:43Z")

</div>

Karl B. wrote:

> The question is if LongDecimal (gem-name: long-decimal) would not be the  
> better match for database fields than  
> BigDecimal, which is more or less a Float with the capability to have  
> more digits.
> 
> Best regards,
> 
> Karl

Karl,  
thank you for you reply,  
the precision is not really a problem for me – the BigDecimal fits my  
needs just fine. The problem is in active record that does not handle  
numbers other then Floats correctly when constructing select queries –  
it puts the parameters in quotes which it should not do. I’m afraid that  
LongDecimal would not be any different in this regard for active record  
(correct me if I’m wrong) thus switching to it would not really help me  
much,

thank you, let me know if you have any other suggestions…

Dmitry
