in reply to "Commifying" a number

Read the FAQ.

Abigail

Replies are listed 'Best First'.
Re: Re: "Commifying" a number
by Tanalis (Curate) on Dec 12, 2002 at 13:02 UTC
    Update: Unwhoops, I thought I'd seen RE: regexp for adding commas to a number before .. it works fine for anything over +/- 1000, but fails for 3 digit numbers or less (ie, if it can't commify, it won't return anything).

    If anyone can think of a fix to that regexp, that'd be good too .. *grins*.

    As far as the specific Q&A page goes, I'm not sure that it's relevant anyway - I thought I was asking something fairly specific (suggestions for an improvement to something I already have, not new code). Maybe I'm wrong ..

    -----

    Eep .. whoops, my mistake. I'd searched the FAQ, which turned up nothing, but evidently word order plays a bigger part than I realised .. :)

    Thanks anyway ..
    --Foxcub

      Listen to Abigail, it is indeed FAQ.

      `perldoc -q comma' yields

      Found in C:\Perl\lib\pod\perlfaq5.pod How can I output my numbers with commas added? This one will do it for you: sub commify { local $_ = shift; 1 while s/^([-+]?\d+)(\d{3})/$1,$2/; return $_; } $n = 23659019423.2331; print "GOT: ", commify($n), "\n"; GOT: 23,659,019,423.2331 You can't just: s/^([-+]?\d+)(\d{3})/$1,$2/g; because you have to put the comma in and then recalculate +your position. Alternatively, this code commifies all numbers in a line regardless of whether they have decimal portions, are prec +eded by + or -, or whatever: # from Andrew Johnson <ajohnson@gpu.srv.ualberta.ca> sub commify { my $input = shift; $input = reverse $input; $input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; return scalar reverse $input; }
      How to RTFM is a wonderful guide which will introduce you to various perl resources.


      MJD says you can't just make shit up and expect the computer to know what you mean, retardo!
      ** The Third rule of perl club is a statement of fact: pod is sexy.

        Tell me I'm wrong, downvote me if you like, but I was asking for an opinion on working code I already have, which, I should point out is remarkably similar to perldoc. Could this be because I've already read it, perhaps?

        I reiterate: I was merely asking if there is a cleaner, or simpler, way to do it. I should probably point out that I'm not necessarily after a regexp solution.

        I don't see how, or why, this is an issue, and spawns negative (in comment value, not in rep, I haven't voted on either and wouldn't know) posts like the two above.

        RTFM isn't always an answer. See the FAQ isn't always an answer. Posting perldoc is definitely not a solution, IMO.

        I quote myself from the original node:

        .. while it works fine .. I'm interested to know if there's a "simpler" way to do this ..

        That's not "How do I do this?". It's not "Help, this doesn't work". I'm asking opinion, asking if, maybe, anyone has thought of some way to do something that others haven't.

        I'm sorry for having an outlook different to that of the "accept code, use it, there's obviously no other way" attitude. I've always thought it better to ask questions to help understanding rather then blindly accepting what's written.

        *sigh* Oh well, eh, it's nearly the weekend.

        -- Foxcub

        Really? It just seems to "eat" the number on my system .. using the following code:
        my $number = 123; $number =~ s/(\d)(?=(\d{3})+(\D|$))/$1\,/g; print $number;

        I get no output from the print, almost as if $number if undef. If I use 1234 as the input, I get 1,234 out - which is right ..

        That's with Perl 5.004.04 on Solaris .. maybe it's a version thing.

        -- Foxcub