Reputation: 1761
I have a product column like so:
t.decimal :price, :precision => 12, :scale => 2
and it keeps returning 500.0 instead of the expected two decimal 500.00. Then I tried manually changing the product price via the console and it'll only save 1 decimal place.
>product.price = 500.00
=> 500.0
>product.price
=> #<BigDecimal:7ff479c6ba40,'0.5E3',9(36)>
How do I get the decimal column to save and return two decimal places?
Upvotes: 1
Views: 2230
Reputation: 2773
scale
specifies number of digits after the decimal point preserved in the database. Saving 1.234
will round to 1.23
, 500.00
will be stored as 500.00
.
In Ruby on Rails, they will be represented as a BigDecimal
. The BigDecimal
will not know about the format that was used in the DB. If its value is 500.0
, its to_s
method will output it as 500.0
because that's accurate enough.
To format the values as currency, use the number_to_currency
helper method.
Also see Ruby on Rails: best method of handling currency / money and Does Ruby have any number formatting classes?
Upvotes: 6