I have a model product, that model have attribute price with decimal type (:precision => 20, :scale => 2
)
In the form I use format for price field that looks like : 3.000,00
, When submitting the form, price change to 3.00
. I want the price to be 3,000.00
, not 3.00
Test with rails console
irb(main):001:0> p = Product.create(:price => "3.000,00") ←[1m←[36m
(0.0ms)←[0m ←[1mBEGIN←[0m ←[1m←[35mSQL (72.0ms)←[0m INSERT INTO
"products" ("created_at", "price" "updated_at") VALUES ($1, $2, $3)
RETURNING "id" [["created_at", Tue, 29 Apr 2 014 14:07:25 WIB
+07:00], ["price", #<BigDecimal:4c218f0,'0.3E1',9(18)>], ["updated_at", Tue, 29 Apr 2014 14:07:25 WIB +07:00]] ←[1m←[36m
(11.0ms)←[0m ←[1mCOMMIT←[0m
=> #<Product id: 8, price: #<BigDecimal:4bd5588,'0.3E1',9(18)>, created_at: "20 14-04-29 07:07:25", updated_at: "2014-04-29 07:07:25">
irb (main):002:0> p.price.to_s
=> "3.0"
I have tried with gsub
for change comma to point on before_save
and store to database, but price always change to 3.00
on database.
before_save :comma_to_delimiter
def comma_to_delimiter
self.price = self.price.gsub(/[.,]/, '.' => ',', ',' => '.')
end
I want like this :
when I input 3.000,00
to field price, on database 3,000.00
and on view price 3.000,00
I can't figure out how to do that. Thanks
Note : I'm using jquery-maskMoney for field price with format :
$("#demo4").maskMoney();
<input type="text" id="demo4" data-thousands="." data-decimal="," data-prefix="R$ " />