Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
laziness, impatience, and hubris
 
PerlMonks  

RE: RE: regexp for adding commas to a number

by AltBlue (Chaplain)
on Aug 17, 2000 at 19:45 UTC ( [id://28363]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to RE: regexp for adding commas to a number
in thread regexp for adding commas to a number

great regex! me too have forgoten about the look-ahead assertion :( my function that does number beautifying has no less than 8 lines :((
still.. there's a little problem when handling floats: the digits after the dot shouldn't be 'beautified' :(
.. i tried to enhance a little your line but the problem still remains because of the fixed-width look-behind:

$number =~ s/(?<!\.\d)(\d)(?=(\d{3})+(\D|$))/$1,/g;
(this example works for numbers with 5 digits after the dot... variations may me done by modifying the 'quantity' of \d from the look-behind assertion)

couldn't think at anything better now.. maybe you have another bright idea for this too o=)

so, after inserting your wizcraft, my lame tool looks something like this:

$number =~ s/(\d+)(\.\d+)?/bea_int($1).$2/eg; sub bea_int { my $kk = $_[0]; $kk =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; return $kk; }

--
AltBlue... w8ing 4 a better solution o=Q

Replies are listed 'Best First'.
RE: open ended floats
by bastard (Hermit) on Aug 17, 2000 at 21:00 UTC
    The easy way to deal with open ended floating pointed nums is to simply remove the floating part until the commas are added, then reattach.
    $integer =~ s/(.*)(\.\d\d+)$/$1/; $float = $2; # do stuff $integer .= $float;
      It would be more efficient to use split.
      my( $int, $float ) = split /\./, $num;
      lol. isn't that what i'm currently doing ?! :))
      plz check up again my previous post and look at the final code - it does exactly that :)
      your snippet doesn't work for 'real' strings like: 'for every 1234.567 blah stuff there is some 34% rate of foobar success at every 9876543.876543 seconds'

      --
      AltBlue.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://28363]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.