Working with Decimals in Ruby on Rails 3

Working with decimals in Ruby on Rails 3

total_net_revenue / total_sold

or

total_net_revenue / total_sold / 100.0

or

total_net_revenue.to_f / total_sold / 100

These three methods give an increasing amount of precision, if you want it. Remember that "average price" is "average price / sale. That's money-per-item so you will want to do the division in that specific order.

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

Why decimal holding value like 0.219e3 instead of 219.0 which I want

When the column type is Decimal, the adapter is probably converting the value to a BigDecimal object on ruby.

require 'bigdecimal'

BigDecimal(219)
=> 0.219e3

The number is actually "219", it's just cientific notation (0.219 x 10^3).

BigDecimal(219) == 219
=> true

BigDecimal(219).to_f
=> 219.0

Decimal values are truncating with to_f

Can I know what will be the reason the to_f automatically reduce the limit of the decimal?

The reason is the to_f methods are used to convert objects to Floats, which are standard 64-bit double precision floating point numbers. The precision of these numbers is limited, therefore the precision of the original object must be automatically reduced during the conversion process in order to make it fit in a Float. All extra precision is lost.

It looks like you are using the BigDecimal class. The BigDecimal#to_f method will convert the arbitrary precision floating point decimal object into a Float. Naturally, information will be lost during this conversion should the big decimal be more precise than what Floats allow. This conversion can actually overflow or underflow if limits are exceeded.

I was just thinking about some truncate with some limit

There is a truncate method if you'd like explicit control over the precision of the result. No rounding of any kind will occur, there is a separate method for that.

  • BigDecimal#truncate

    Deletes the entire fractional part of the number, leaving only an integer.

    BigDecimal('3.14159').truncate #=> 3
  • BigDecimal#truncate(n)

    Keeps n digits of precision, deletes the rest.

    BigDecimal('3.14159').truncate(3) #=> 3.141

How do you round a float to 2 decimal places in JRuby?

Float#round can take a parameter in Ruby 1.9, not in Ruby 1.8. JRuby defaults to 1.8, but it is capable of running in 1.9 mode.

Ruby: Rounding float in Ruby

When displaying, you can use (for example)

>> '%.2f' % 2.3465
=> "2.35"

If you want to store it rounded, you can use

>> (2.3465*100).round / 100.0
=> 2.35

Rails 3 Migration Alter Float Length/Decimal

Personal experience what works best (since MySQL/sqlite sometimes refuses changes to columns): Create a new column, copy the data, delete the old column, rename the new column.

# Example for latitude

add_column :skateparks, :latitude2, :decimal, :precision => 15, :scale => 12
execute "UPDATE skateparks SET latitude2 = latitude"
remove_column :skateparks, :latitude
rename_column :skateparks, :latitude2, :latitude

EDIT: On the second look :float, { :length => 15, :decimals => 12 } seems to be wrong. I assume you meant: :decimal, :precision => 15, :scale => 12?

Ruby on Rails decimal comparison stopped working

It occurs because state.database_version is an instance of BigDecimal class. This article explain why it is BigDecimal.

Look at this example:

BigDecimal('0.56e0')
=> 0.56e0
irb(main):008:0> BigDecimal('0.56e0') == 0.56
=> false
irb(main):009:0> BigDecimal('0.56e0').to_f
=> 0.56

As you can see 0.56e0 after transformation to float type becomes 0.56 and your comparison returns true.

Nate explained more briefly why it's happening in this comment.



Related Topics



Leave a reply



Submit