Decimal


#1

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.


#2

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

On 5/4/06, M. removed_email_address@domain.invalid wrote:

Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


#3

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 wrote:

Any help is appreciated.


Jon Gretar B.
http://www.jongretar.net/


#4

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