Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Formatting number

by nipper (Initiate)
on Jul 31, 2003 at 18:01 UTC ( [id://279693]=perlquestion: print w/replies, xml ) Need Help??

nipper has asked for the wisdom of the Perl Monks concerning the following question:

Hi- I'm quite new at Perl, and I'm trying to format a number. I'm doing simple arithmetic on numbers, i.e., adding 25000 and 34000. How do I format the total into a format such as 34,000 and 25,000? I've looked at printf() and sprintf() and they don't seem to accomodate this format. Any help would be appreciated. -rob

Replies are listed 'Best First'.
Re: Formatting number
by blue_cowdawg (Monsignor) on Jul 31, 2003 at 18:06 UTC

    Take a gander at Number::Format. If I remember correctly it does locale sensitive number formatting including among other things inserting commas at thousand boundaries.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Test the code? We don't need to test no stinkin' code! All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

Re: Formatting number
by jsprat (Curate) on Jul 31, 2003 at 18:07 UTC
    perldoc -q commas at a command prompt

    or look at perlfaq5 and search for "How can I output my numbers with commas added?"

      Thanks, all! The perldocfaq listed a regexp to use, so I don't have to install modules, etc. Thanks again. -nipper
Re: Formatting number
by Limbic~Region (Chancellor) on Jul 31, 2003 at 18:08 UTC
    nipper,
    Welcome to the Monastery! this node from our Categorized Questions and Answers section seems to answer what you are looking for. I highly recommend reading the PerlMonks FAQ since there is a myriad of information and it can be a little overwhelming when you are first geting started.

    Cheers - L~R

Re: Formatting number
by nipper (Initiate) on Jul 31, 2003 at 19:26 UTC
    Thanks, all! The perlfaq5 listed a regexp to use, so I don't have to install modules, etc. Thanks again. -nipper
Re: Formatting number
by Anonymous Monk on Jul 31, 2003 at 19:02 UTC
    try this $num=25000 $num =~ s/0/\,0/; yak
      That will only work if the first zero to occur is exactly where the comma should go:
      $ perl -e '$num=10; $num =~ s/0/\,0/; print $num,"\n";'
      1,0
      $ perl -e '$num=25500; $num =~ s/0/\,0/; print $num,"\n";' 
      255,00
      $ perl -e '$num=250000000; $num =~ s/0/\,0/; print $num,"\n";'
      25,0000000
      

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2024-04-23 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found