Forum: Ruby on Rails Decimal

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
M. (Guest)
on 2006-05-04 17:56
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.
Jón B. (Guest)
on 2006-05-04 18:13
(Received via mailing list)
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
>


--
Jón B. (Guest)
on 2006-05-04 18:22
(Received via mailing list)
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/
>


--
Karl B. (Guest)
on 2006-05-05 00:58
(Received via mailing list)
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
This topic is locked and can not be replied to.