How to make a price of 0.00 instead of 0.0?

my users can enter and create prices. I wanted to know how to do this when users enter 23.5 and instead get 23.50 or 0.0 get 0.00. How to add to t.decimal or my price: decimal the following abilities?

Thanks for the help!

Answer (: price with scale and accuracy)

class CreatePrices < ActiveRecord::Migration
  def self.up
    create_table :prices do |t|
      t.string :price_name
      t.decimal :price, :precision => 10, :scale => 2
      t.date :date

      t.timestamps
    end
  end

  def self.down
    drop_table :prices
  end
end

schema.rb:

  create_table "prices", :force => true do |t|
    t.string   "price_name"
    t.decimal  "price",      :precision => 10, :scale => 2
    t.date     "date"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
  end

my lining form:   <% = f.label: price%>
  <% = f.text_field: price%>

Then in your view, enter number_to_currency (@ model.attribute):

<B> Price:

+3
source share
3 answers

Try using the option :scalein your migration. Use scale 2 if you want two digits to the right of the decimal point, for example:

 t.decimal :price, :precision => 10, :scale => 2
+5

:

def price #in price model to set price field
 number_to_currency(self[:price], :unit => '')
end

:

t.float :price, :default => '1.00'

:

f.text_field :price
+4

I ended up doing scale and precision, but also added a number_to_currency method in the view.

+1
source

All Articles