decimal - Ruby on Rails - Currency : commas causing an issue -
Looking at SO, I think the preferred method of currency using ROR is using decimal (8,2) and using number_to_currency () to output them;
I can get my number outside of DB, but I am having problems in getting them in.
In my update action I have the following line.
parameter shows the correct value if p>
inserted @ non_labor_expense.update_attributes (parameter [: non_labor_expense]). Xx, yyy.zz, but which gets accumulated in DB is only xx.00 I have to do to pay attention to the fact that there may be a comma and no user can enter can. (Cents). For some regex and commas? If you will handle decimal, it 2 2 versus .20
There is a builtin or at least should be a better way.
My migrations (I do not know if this helps):
class ChangeExenseToDec & lt; ActiveRecord :: Migration DF self.up change_column: non_labor_expenses ,: amount,: decimal ,: accuracy => 8 ,: scale = & gt; 2 end DEF self.down change_column: non_labor_expenses ,: zodiac: integer end end
It may depend on the DBMS you are using, but as far as I know, decimal areas will not be accepted as a comma (at least not as partitions); Any method may be that the database has a comma Rather than a point to be accepted as a decimal point). What you need to do is drop commas (perhaps, in a before_save
or before_validation
filter), remove from your number, and then when you display numbers, add the comma back to < / P>
before_validation:. strip_commas_from_non_labor_expense def strip_commas_from_non_labor_expense self.non_labor_expense = self.non_labor_expense.to_s.gsub (/, /, '') end .to_f
However, such expenditure comma-separated groups and two using Mentioned as wanting to appear formatted with decimal places:
<% non_labor_expense = ... # get puts value number_to_currency from your model (Non_labor_expense ,: exact = & Gt; 2 ,: Separator =>, ')%>