Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby and money, in a rails app, how to store money values in the db?

I want to make sure I don't have rounding issues when it comes to storing prices for products in a rails app.

What mysql datatype should I use, and what does it map to in rails?

I want decimal with 10 places for precision.

like image 808
Blankman Avatar asked Feb 21 '11 23:02

Blankman


1 Answers

Sounds you want the :decimal column type. You can control the total number of digits and the number of decimal places with the :precision and :scale options:

add_column :mytable, :mycolumn, :decimal, :precision => 30, :scale => 10

A little more on data types in this documentation (no idea what the column function is, though - probably internal!).

Hope this helps!

like image 125
Xavier Holt Avatar answered Sep 20 '22 17:09

Xavier Holt