Ask a Question related to Ruby, Design and Development.
-
Chris Morris #1
format number with comma separators?
I'm brain dead and just trying to get formatted numbers in a task that's
already 10 tangents deep -- argh. Anyway, how can I sprintf (or
otherwise) this:
456778904
to this:
456,778,904
--
Chris
[url]http://clabs.org/blogki[/url]
Chris Morris Guest
-
Format Phone Number
I have a text feild (homephone) in MSAccess that hold phone numbers (410)543-3333 When I display these in a <CFOUTPUT>#HOMEPHONE#<\CFOUTPUT> the... -
Format a number on SELECT
I know Fusion has a function called NumberFormat() for formatting numbers on the fusion side during display, but I was wondering if I can select the... -
How to format Integer to include comma
which function do I use to format an integer value to include a comma? Or is there a currency function that will take 2500 and output $2,500? -
Number Format
Hi, In the FrontPage Database Wizard I have the following custom query: Select *, FORMAT(Number, '#.00') AS fNumber FROM MyFile When I view... -
format number into currency format ($1,000,000.00)
SELECT '$' + CONVERT(VARCHAR, CONVERT(MONEY, 1000000), 1) "lamP" <phantlam@yahoo.com> wrote in message... -
Mark J. Reed #2
Re: format number with comma separators?
On Tue, Aug 05, 2003 at 11:55:12PM +0900, Chris Morris wrote:
Unless there's something I don't know about - a distinct possibility> I'm brain dead and just trying to get formatted numbers in a task that's
> already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> otherwise) this:
>
> 456778904
>
> to this:
>
> 456,778,904
- there's no built-in function to do this. You can, however,
do it with a regex. Assuming the numbers are all integers (no
decimal points), then this will work:
formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
-Mark
Mark J. Reed Guest
-
Robert Klemme #3
Re: format number with comma separators?
"Mark J. Reed" <markjreed@mail.com> schrieb im Newsbeitrag
news:20030805150600.GD28141@mulan.thereeds.org...that's> On Tue, Aug 05, 2003 at 11:55:12PM +0900, Chris Morris wrote:> > I'm brain dead and just trying to get formatted numbers in a taskThis fails for negative numbers in the range -100..-999 and all other>> > already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> > otherwise) this:
> >
> > 456778904
> >
> > to this:
> >
> > 456,778,904
> Unless there's something I don't know about - a distinct possibility
> - there's no built-in function to do this. You can, however,
> do it with a regex. Assuming the numbers are all integers (no
> decimal points), then this will work:
>
> formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
negative numbers with an amount of digits that is divisable by 3.
Alternative:
def format(num)
s = num.to_s
if s.include? ?.
pre, post = s.split '.'
"#{pre.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse}.#{post}"
else
s.reverse.gsub( /\d{3}(?=\d)/, '\&,' ).reverse
end
end
robert
Robert Klemme Guest
-
Robert Klemme #4
Re: format number with comma separators?
"Chris Morris" <chrismo@clabs.org> schrieb im Newsbeitrag
news:3F2FCA9F.4000600@clabs.org...Due to probs with negative numbers I added another version to that.> Chris Morris wrote:
>> Found this: [url]http://www.rubygarden.org/ruby?FixNumFormat[/url]> > I'm brain dead and just trying to get formatted numbers in a task
> > that's already 10 tangents deep -- argh. Anyway, how can I sprintf (or
> > otherwise) this:
> >
> > 456778904
> >
> > to this:
> >
> > 456,778,904
> >
robert
Robert Klemme Guest
-
Tom Copeland #5
Re: format number with comma separators?
On Tue, 2003-08-05 at 12:07, Warren Brown wrote:
Immortalized here:> If you want to allow a decimal point:
>
> irb(main):001:0> puts "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
> -123,456,789.01
>
> - Warren Brown
[url]http://rubyforge.org/snippet/detail.php?type=snippet&id=8[/url]
Yours,
tom
Tom Copeland Guest
-
Xavier Noria #6
Re: format number with comma separators?
On Tuesday 05 August 2003 18:07, Warren Brown wrote:
But:>> >> >> Yet another regex solution:
> >>
> >> % ruby -e 'puts "456778904".gsub(/(.)(?=.{3}+$)/, %q(\1,))'
> >> 456,778,904
> >>
> >> -- fxn
> >>
> >> .gsub(/(\d)(?=\d{3}+$)/, '\1,')
> > That spurious line at the end uses a regex that works for negative
> > numbers as well.
> If you want to allow a decimal point:
>
> irb(main):001:0> puts "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,
> '\1,') -123,456,789.01
irb(main):003:0> puts "-123456789.0123".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/, '\1,')
-123,456,789.0,123
Is that the expected behaviour? Which is the convention in the decimal part?
-- fxn
Xavier Noria Guest
-
Warren Brown #7
Re: format number with comma separators?
> irb(main):001:0> puts
Whoops,> "-123456789.01".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,'\1,')
> -123,456,789.01
irb(main):001:0> puts
"-123456789.012345".gsub(/(\d)(?=\d{3}+(\.\d*)?$)/,'\1,')
-123,456,789.012,345
How about:
irb(main):028:0> puts
"-123456789.123456789".gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2')
-123,456,789.123456789
- Warren Brown
Warren Brown Guest



Reply With Quote

