Ruby number_to_currency displays a totally wrong number?

Until you find a fix, you can make your own :) def formatted_number(n) a,b = sprintf("%0.2f", n). Split('. ') a.

Gsub!(/(\d)(?=(\d{3})+(?! \d))/, '\\1,') "$#{a}. #{b}" end DATA.

Each do |n| puts formatted_number(n) end __END__ 1 1.1 1.11 11 11.1 11.11 111 111.1 111.11 1111 1111.1 1111.11 11111 11111.1 11111.11 111111 111111.1 111111.11 1111111 1111111.1 1111111.11 11111111 11111111.1 11111111.11 111111111 111111111.1 111111111.11 Output $1.00 $1.10 $1.11 $11.00 $11.10 $11.11 $111.00 $111.10 $111.11 $1,111.00 $1,111.10 $1,111.11 $11,111.00 $11,111.10 $11,111.11 $111,111.00 $111,111.10 $111,111.11 $1111,111.00 $1111,111.10 $1111,111.11 $1,1111,111.00 $1,1111,111.10 $1,1111,111.11 $11,1111,111.00 $11,1111,111.10 $11,1111,111.11.

Another (more fancy) solution that might help you until the bug is resolved def formatted_number(n, options={}) options = { :precision => 2, :separator => '. ', :delimiter => ',', :format => "$%s" }. Merge(options) a,b = sprintf("%0.

#{options:precision}f", n). Split('. ') a.

Gsub!(/(\d)(?=(\d{3})+(?! \d))/, "\\1#{options:delimiter}") sprintf(options:format, "#{a}#{options:separator}#{b}") end DATA. Each do |n| # default $1,234.56 puts formatted_number(n) # precise $0.12345 puts formatted_number(n, :precision => 5) # European 1.234,56 EUR puts formatted_number(n, :separator => ',', :delimiter => '.', :format => '%s EUR') # line break puts '' end __END__ 5.5966 52.5344 3021.565 34809.48 25924.567342 Output $4.563 $5.59660 5,60 EUR $52.53 $52.53440 52,53 EUR $3,024.563 $3,021.56500 3.021,57 EUR $34,809.48 $34,809.48000 34.809,48 EUR $25,924.567 $25,924.567342 25.923,68 EUR.

Smotchkkiss I tried both suggestions. They both did the same thing number_to_currency does on my system. I suspect this is something in the Ruby version I'm running.

I spent most of the day trying to get a newer version of Ruby installed on my system, and finally gave up and did a complete system restore from Time Machine. I'm still learning the Mac's quirks and I really managed to mess up my RubyGems installation. @Andrew Grimm I would love to leave this as a comment.

I see no way to do so (running Snow Leopard, Firefox w NoScript). I spent at least 30 minutes going thru the FAQ, trying to find a way to leave a comment instead of an answer. No Go.

And this is an answer, at least a partial answer. I managed to load a newer version of Ruby (ruby 1.8.7 (2010-01-10 patchlevel 249)), and the problem disappeared. It is apparently a bug in Ruby.

Now I just have to figure out how to install a newer version of Ruby into system on my MacBook and the 2 minis I've been using as test servers.

I ran into the same problem. It seems like the issue is in the translation of a Big Decimal to a float. I changed one line in the formatted_number method and was able to get the number to appear as it should: a,b = sprintf("%0.

#{options:precision}f", n). Split('. ') was changed to a,b = sprintf("%0.

#{options:precision}f", n. To_s. To_f).

Split('. ') Converting to a string first and then to a float did the trick - it's hokey but until the underlying issue gets resolved it will work for us.

I ended up loading the rvm gem on all of my Macs, and I'm now running ruby 1.8.7 p249. That solved the problem for me. I did verify the bug in Ruby, and I found this for more info: ruby-forum.com/topic/189053#827091 cihaks - It sounds like the temporary patch they suggested is very similar to what you're doing.In any case, thanks for the post and the info.

:-).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions