Ruby on Rails - Currency : commas causing an issue.

Posted by easement on Stack Overflow See other posts from Stack Overflow or by easement
Published on 2010-01-25T19:16:23Z Indexed on 2010/03/29 1:43 UTC
Read the original article Hit count: 265

Looking on SO, I see that the preferred way to currency using RoR is using decimal(8,2) and to output them using number_to_currency();

I can get my numbers out of the DB, but I'm having issues on getting them in.

Inside my update action I have the following line:

if @non_labor_expense.update_attributes(params[:non_labor_expense]) 
puts YAML::dump(params) 

The dump of params shows the correct value. xx,yyy.zz , but what gets stored in the DB is only xx.00

What do I need to do in order to take into account that there may be commas and a user may not enter .zz (the cents). Some regex and for comma? how would you handle the decimal if it were .2 versus .20 .

There has to be a builtin or at least a better way.

My Migration (I don't know if this helps):

class ChangeExpenseToDec < ActiveRecord::Migration
    def self.up
       change_column :non_labor_expenses, :amount, :decimal, :precision => 8, :scale => 2
    end

    def self.down
          change_column :non_labor_expenses, :amount, :integer
    end
end

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about currency