bigdecimal - Can't get decimals to display in text field for Rails 3 -


confused rails 3 newbie. model has purchase_price attribute defined this:

t.decimal :purchase_price, :precision => 12, :scale => 2, :default => 0.00 

my goal have simple rails 3 app can view , edit value. in model, defined custom getter force 2 decimal places exist.

def purchase_price   sprintf( "%.2f",attributes['purchase_price']) end 

this works correctly console , when display show page. on edit page have text field edit value. problem if value saved '123.00' value displayed '123.0'.

questions:

  1. why custom getter not getting called?
  2. why displayed 1 decimal , not 2 decimals?

update

  1. decimal value stored in mysql: 100.20
  2. value returned console: 100.20
  3. value displayed in text field: 100.2
  4. value returned via firebug: "100.2"
  5. the text field length not issue.
  6. if value in db 100.21, displayed correctly 100.21

try this

f.text_field :purchase_price, :value=>number_to_currency(f.object.purchase_price, :unit=>'') 

that should convert number currency (with 2 decimal places) , :unit=>'' exclude "$".

and i'd recommend removing custom purchase_price method , using number_to_currency helper needed instead. overriding purchase_price accessor isn't bad idea confuse things might expecting number rather string.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -