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.text_field :cost, :class => 'cost' %>
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.text_field :cost, :class => 'cost' %>
You should use
number_with_precision
helper. See doc.Example:
Within you form helper:
BTW, if you really want to display some price, use
number_to_currency
, same page for doc (In a form context, I'd keepnumber_with_precision
, you don't want to mess up with money symbols)Rails has a number_to_currency helper method which might fit you specific use case better.
Alternatively, you can use the format string
"%.2f" % 1.5
. http://ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.sprintfFor this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.
You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org