Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: "Commifying" a number

by PodMaster (Abbot)
on Dec 12, 2002 at 14:24 UTC ( [id://219324]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: "Commifying" a number
in thread "Commifying" a number

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.

Replies are listed 'Best First'.
Re: Re: Re: Re: "Commifying" a number
by Tanalis (Curate) on Dec 12, 2002 at 14:54 UTC
    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

      Eh?

      Maybe i'm confused, but I did not respond to your SOPW. I responded to your reply to Abigail's reply to your SOPW. FAQ is perldoc perlfaq.

      Furthermore, the FAQ is a simpler way to do this, and if you had read the faq, you should've said so, but that's all besides the point, don't get bent out of shape.


      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://219324]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found