Hello list, This sounds like a really simple question, but I haven''t been able to come up with an answer after searching the Ruby docs, Rails sample code in the Agile book, or Google. I have a number such as ''1234567.89''. What is the printf format string to let me display it as ''$1,234,567.89''? If I use printf("$%0.2f"), I get ''$1234567.89'' (note: no commas separating every three digits). I''m 99% sure I''ve done this before in C in a single format string - without having to e.g. print (x / 1000).int , insert a comma, then print (x % 1000) - but can''t for the life of me remember how I did it, nor find any reference online. Such a simple problem - I bet most of you will laugh at the question - but how do you do it?? Thanks in advance Dave M.
> I have a number such as ''1234567.89''. What is the printf format > string to let me display it as ''$1,234,567.89''? > > If I use printf("$%0.2f"), I get ''$1234567.89'' (note: no commas > separating every three digits).Some printf implementatations understand the '' flag. printf("$%''.2f", <bla>) would give you a thousands seperator based on the current locale setting. For as far as i know the ruby printf doesn''t have this, so you''ll have to write some formatting code yourself.> I''m 99% sure I''ve done this before in C in a single format string - > without having to e.g. print (x / 1000).int , insert a comma, then > print (x % 1000) - but can''t for the life of me remember how I did it, > nor find any reference online. >I did. ;) Google rulez: http://www.rubygarden.org/ruby?NumericFormat Which has a bit of code that handles just that: class Numeric def format(separator = '','', decimal_point = ''.'') num_parts = self.to_s.split(''.'') x = num_parts[0].reverse.scan(/.{1,3}/).join(separator).reverse x << decimal_point + num_parts[1] if num_parts.length == 2 return x end def Numeric.format(number, *args) number.format(*args) end end Maarten.
Hello list, This sounds like a really simple question, but I haven''t been able to come up with an answer after searching the Ruby docs, Rails sample code in the Agile book, or Google. I have a number such as ''1234567.89''. What is the printf format string to let me display it as ''$1,234,567.89''? If I use printf("$%0.2f"), I get ''$1234567.89'' (note: no commas separating every three digits). I''m 99% sure I''ve done this before in C in a single format string - without having to e.g. print (x / 1000).int , insert a comma, then print (x % 1000) - but can''t for the life of me remember how I did it, nor find any reference online. Such a simple problem - I bet most of you will laugh at the question - but how do you do it?? Thanks in advance Dave M. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Maarten Engelen wrote:> > I have a number such as ''1234567.89''. What is the printf format > string to let me display it as ''$1,234,567.89''? >Don''t know the printf format, but Rails has a nice helper: number_to_currency(1234567890.50) => $1,234,567,890.50 Check out http://ap.rubyonrails.com (look for ActionView::Helpers::NumberHelper) -- Best Karol Hosiawa
Why don''t you do this... number_to_currency(1234567890.506) => $1,234,567,890.51 --bryce http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000333
Don''t know about the printf way but have a look at the number_to_currency or number_with_delimiter methods. they may be what you need: http://api.rubyonrails.com/classes/ActionView/Helpers/NumberHelper.html On 7/22/05, Maarten Engelen <maarten-i0jN7yJ10GVBDgjK7y7TUQ@public.gmane.org> wrote:> Hello list, > > This sounds like a really simple question, but I haven''t been able to > come up with an answer after searching the Ruby docs, Rails sample > code in the Agile book, or Google. > > I have a number such as ''1234567.89''. What is the printf format > string to let me display it as ''$1,234,567.89''? > > If I use printf("$%0.2f"), I get ''$1234567.89'' (note: no commas > separating every three digits). > > I''m 99% sure I''ve done this before in C in a single format string - > without having to e.g. print (x / 1000).int , insert a comma, then > print (x % 1000) - but can''t for the life of me remember how I did it, > nor find any reference online. > > Such a simple problem - I bet most of you will laugh at the question - > but how do you do it?? > > Thanks in advance > > Dave M. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
It seems everybody has to roll their own code to format numbers. Is there anything standard -- comparable to java''s DecimalFormat class?
Aha! That''s what I was looking for! Thanks Bryce Dave M. On 7/23/05, bryce benton <brycebenton-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Why don''t you do this... > > number_to_currency(1234567890.506) => $1,234,567,890.51 > > --bryce > > http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#M000333 > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >