Rails 3. How to display two decimal places in edit form?
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)
Rails 3. How to explicitly round a number to two decimal places in the model?
You should try using the :decimal
type of database field, with the :scale
set to 2
To add a decimal column to a new table;
create_table :my_table do |t|
t.decimal :my_column, :scale => 2
end
To add a column to an existing table;
add_column :my_table, :my_column, :decimal, :scale => 2
It is wise to have precisions since some database does not have precision defaults. Such as postgresql:
add_column :my_table, my_column, precision: 30, scale: 2
Displaying an output in Ruby to 2 decimal places
You have to add a dot in the format to say you want the 2
to be the precision after the decimal dot:
s = '%.2f' % @price
Rails currency with 2 decimal
You could use a helper called: number_with_precision
it would look like this:
<p class="price"><%= number_with_precision(product.price_with_currency, :precision => 2) %></p>
read more here: Rails 3. How to display two decimal places in edit form?
Can't get decimals to display in text field for Rails 3
Try this
f.text_field :purchase_price, :value=>number_to_currency(f.object.purchase_price, :unit=>'')
That should convert the number to a currency (with two decimal places) and the :unit=>''
will exclude the "$".
And I'd recommend removing your custom purchase_price
method and using the number_to_currency
helper where needed instead. Overriding the purchase_price
accessor isn't a bad idea but it could confuse some things that might be expecting a number rather than a string.
How to specify decimal precision in a simple_form number field (Rails)
If you can do it with formtastic you can usually do it with Simple Form in my experience. Try this:
<%= f.input :sales_price, :input_html => {value: number_with_precision(f.object.sales_price, precision: 2) } %>
If using an input_field, then you don't need the :input_html
:
<%= f.input_field :sales_price, value: number_with_precision(f.object.sales_price, precision: 2) %>
Saving a number with specific decimal places to a variable
The method number_with_precision
is a helper method for views only, and it returns a string version of the number. It doesn't return a number with the specified precision, if that's what you're looking for. The good news is, if you want exactly what number_with_precision
gives you, you can use sprintf
.
sprintf('%.2f', 3.1415926) # result: "3.14"
sprintf
returns a value you can store, instead of printing to stdout like printf
does. So you can easily capture it:
number = sprintf('%.2f', 3.1415926)
If you wanted to treat it as a number, you could probably build a class that does this for you, and converts to and from a Float as needed.
Related Topics
How to Encode Media in Base64 Given Url in Ruby
Why Did Matz Choose to Make Strings Mutable by Default in Ruby
Do Ruby 'Require' Statements Go Inside or Outside the Class Definition
Ruby on Rails Rmagick on Windows 7
Heroku and Rails: Gem Load Error with Postgres, However It Is Specified in Gemfile
Welcome/Home Page in Ruby on Rails - Best Practice
Using Factory_Girl in Rails with Associations That Have Unique Constraints. Getting Duplicate Errors
What Are the Paths That "Require" Looks Up by Default
Gem Install Error (Sass Compass)
How to Configure Ruby on Rails with Oracle
How to Add Migration with Multiple References to the Same Model in One Table? Ruby/Rails
Is It Possible for Rspec to Expect Change in Two Tables
How to Iterate Activerecord Attributes, Including Attr_Accessor Methods
Circular Dependency Detected While Autoloading Constant User
How to Sort a Ruby Hash by Number Value
How to Get Readline Support in Irb Using Rvm on Ubuntu 11.10