views:

135

answers:

3

hi friends,

iam working on RoR code, i want to know how to find two digit precision for the numbers

a = 0.7

b = 5

thanks

+2  A: 

number_with_precision. If you're looking to express them as currency, use number_to_currency.

Ben
A: 

If you're looking to store those numbers and their precision in the database you should set up decimal fields.

 add_column :table_name, :column_name, :decimal,  :precision => 5, :scale => 2

Options for a decimal field:

  • :precision : maximum number of digits in a value
  • :scale : number of digits after the decimal place

With a decimal field you will never need the helper methods to limit the precision after the decimal place.

EmFi
+1  A: 

Always your standard

a = "%.2f" % 0.2
b = "%.2f" % 5

0.20

5.00

Outputs strings though, so you may need ("%.2f" % 0.2).to_i or cast later (a.to_i).

Lukas