开发者

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

开发者 https://www.devze.com 2023-02-08 18:04 出处:网络
I am trying to make a currency value. >> 100.00 I tried this: irb(main):021:0> 100.number_to_currency

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 tr开发者_运维百科ied this :

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

Any ideas? Rails3 + Ruby 1.8.7


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消