3

my users can input and create prices. I wanted to know how to make it so when users input 23.5 they get 23.50 instead or 0.0 they get 0.00. How does one add to the t.decimal or my price:decimal the following ability?

Thank you for the help!

The Answer (:price with scale and precision)

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 scaffolded form: <%= f.label :price %>
<%= f.text_field :price %>

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

Price:

1
  • 1
    Seems like a output problem. Use number_to_currency(price) to display the output. Show the part of the code where the price is displayed.
    – Zabba
    Jun 6, 2011 at 18:04

3 Answers 3

5

Try using the :scale option in your migration. Use a scale of 2 if you want two digits to the right of the decimal point, e.g:

 t.decimal :price, :precision => 10, :scale => 2
7
  • My migration is old, is it still OK to add to it? I put it inside of it and did a db:reset, added the price 23.50 and still got 23.5. Jun 5, 2011 at 19:03
  • 1
    It's ok as long as you're willing to recreate your table (and thus lose the data that's currently in your database) since you'll need to run rake db:migrate:reset to re-run all the migrations. Otherwise simply create a new migration to change the datatype of your price column, e.g. t.change_column :products, :price, :decimal, { :scale => 2, :precision => 10 }. See this link for more info on change_column.
    – mbreining
    Jun 5, 2011 at 19:16
  • 1
    Also check out this post if you're using SQLite3.
    – mbreining
    Jun 5, 2011 at 19:16
  • Yes i am using SQLite3, how would the change column migration look? i get lost at rails generate migration ChangePrice------. Jun 5, 2011 at 19:35
  • 1
    See this and this
    – Zabba
    Jun 5, 2011 at 19:45
4

Without using precision and scale, I did it like this:

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

Migration as:

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

View as:

f.text_field :price
2
  • And you were able to get the price to be 1.00 as is written? i Keep getting 1.0. Jul 5, 2011 at 2:19
  • Did you use number_to_currency in your show.html.erb or anywhere else or does the def price do that automatically when you have it as a float? Jul 5, 2011 at 23:14
1

I ended doing the scale and precision but also adding in the view the number_to_currency method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.