72

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' %>

mecyborg
  • 407
  • 3
  • 12
leonel
  • 10,106
  • 21
  • 85
  • 129

5 Answers5

169

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)


apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Brilliant and thanks! One side note is that using the `|| 0` will prevent any HTML5 placeholder value from being displayed unless the user erases the value. Also, erasing the field may cause issues with numeric validation (because there's an empty value attribute when what you want is _no_ value attribute). Nothing a little JS can't fix, but it seems like Rails should be a tad smarter in this case. – Tom Harrison Sep 18 '12 at 16:57
47

Alternatively, you can use the format string "%.2f" % 1.5. http://ruby-doc.com/docs/ProgrammingRuby/#Kernel.sprintf

stevec
  • 41,291
  • 27
  • 223
  • 311
gkuan
  • 921
  • 6
  • 6
11

For this I use the number_to_currency formater. Since I am in the US the defaults work fine for me.

<% price = 45.9999 %>
<price><%= number_to_currency(price)%></price>
=> <price>$45.99</price>

You can also pass in options if the defaults don't work for you. Documentation on available options at api.rubyonrails.org

codesponge
  • 220
  • 2
  • 8
7

Rails has a number_to_currency helper method which might fit you specific use case better.

Brian
  • 6,820
  • 3
  • 29
  • 27
  • I think the accepted answer is closer -- `number_to_currency` results in a formatted string that Rails cannot cast back to a float. `number_with_precision` is probably the better option. – Tom Harrison Sep 18 '12 at 17:00
  • @TomHarrisonJr sure it can be cast back to a float. `price = "$3.99"; price.slice!("$"); puts price.to_f => 3.99` It just depends on the user experience you want. Some might want the ability to put a dollar sign into the input box. I know I like having the option. – bfcoder Dec 18 '13 at 04:52
0

This seems to work nicely, I added it to application_helper.rb

def display_2dp(form, field)
  val = form.object.send(field)
  unless val.nil?
    return '%.2f' % val 
  end
end

and use it like this:

<%= form.number_field :cost, step: 0.01, value: display_2dp(form, :cost) %>

just replace :cost with the field you're displaying.

Some advantages:

  • It can be reused on any forms in your app
  • It won't accidentally replace a nil with 0 (like what might happen with || 0)
  • It will always display exactly 2 decimal places (for example, 10.0 would appear as "10.00")
stevec
  • 41,291
  • 27
  • 223
  • 311