# Decimal

**URL:** <https://www.ruby-forum.com/t/decimal/94478>\
**Category:** Rails\
**Created:** [May 4, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/decimal/94478 "2006-05-04T15:56:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [May 4, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/decimal/94478/1 "2006-05-04T15:56:54Z")

</div>

Can someone please tell me the best approach to adding support for  
editing MySQL decimal types in rails?

I have a field in the DB that represents currency, and the view will not  
let save anything past the decimal place so, 10.99 becomes 10.00  
automatically.

Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [May 4, 2006, 4:13pm UTC](https://www.ruby-forum.com/t/decimal/94478/2 "2006-05-04T16:13:28Z")

</div>

You have to name it :float instead of :integer in your database.

On 5/4/06, M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [May 4, 2006, 4:22pm UTC](https://www.ruby-forum.com/t/decimal/94478/3 "2006-05-04T16:22:43Z")

</div>

Oh and of course the variables you put into the database have to be  
float also. As Integer in Ruby does not include decimals either. So if  
something is integer you are better of converting to to float before.  
Example @order.price = @number.to\_f + @vat

On 5/4/06, Jon Gretar B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Any help is appreciated.  
> > –
> 
> * * *
> 
> Jon Gretar B.  
> [http://www.jongretar.net/](http://www.jongretar.net/)

–

---

<div class="post-metadata">

**Author:** ![drweb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@drweb](https://www.ruby-forum.com/u/drweb)\
**Post date:** [May 4, 2006, 10:58pm UTC](https://www.ruby-forum.com/t/decimal/94478/4 "2006-05-04T22:58:19Z")

</div>

M. wrote:

> Can someone please tell me the best approach to adding support for  
> editing MySQL decimal types in rails?

You can for example use long-decimal as ruby-type to match mysql  
decimal types with fixed number of digits after the decimal point.

gem install long-decimal

in ruby

require “rubygems”  
require\_gem “long-decimal”

…

> I have a field in the DB that represents currency, and the view will not  
> let save anything past the decimal place so, 10.99 becomes 10.00  
> automatically.

You should be aware that there are other approaches.  
There is a Money-type in Ruby, which might be useful for you.  
I have some doubts if this is the right way to go, because Money  
combines  
the currency and the amount, which could be two different fields in the  
database.  
But it might work in your case.

I would not recommend using Float for monetary stuff, because  
it has uncontrolled rounding behaviour.  
BigDecimal would be a possibility, but it is not an exact match to  
the database type.

Best regards,

Karl
