Rails 3. How to display two decimal places in edit form?

前端 未结 4 2011

I have this edit form.

But when I store something such as 1.5, I would like to display it as 1.50.

How could I do that with the form helper? <%= f.

4条回答
  •  感情败类
    2020-12-04 13:34

    You should use number_with_precision helper. See doc.

    Example:

    number_with_precision(1.5, :precision => 2)
    => 1.50 
    

    Within you form helper:

    <%= f.text_field :cost, :class => 'cost', :value => (number_with_precision(f.object.cost, :precision => 2) || 0) %>
    

    BTW, if you really want to display some price, use number_to_currency, same page for doc (In a form context, I'd keep number_with_precision, you don't want to mess up with money symbols)


提交回复
热议问题