Trip
Trip

Reputation: 27114

How to derive to places past the decimal in Ruby or Rails

I am trying to make a currency value.

>> 100.00

I tried this:

irb(main):021:0> 100.number_to_currency
NoMethodError: undefined method `number_to_currency' for 100:Fixnum
    from (irb):21

I tried this :

irb(main):021:0> 100 * 1000.round.to_f/1000
=> 100.0

Any ideas? Rails3 + Ruby 1.8.7

Upvotes: 0

Views: 658

Answers (1)

Xavier Holt
Xavier Holt

Reputation: 14619

number_to_currency is a Rails helper method - it's not an instance method of Fixnum. In Rails 2.3.8, at least, you can do:

number_to_currency(100)

Although you might need:

number_to_currency(100, :precision => 2)

To get the right number of decimal places. You can change the default, and a bunch more display options, by editing your I18n translation files.

Or, if you just want a quickie that you can run easily from the console, too:

sprintf('%.02f', 100)

Upvotes: 4

Related Questions