Path: utzoo!utgpu!cunews!cognos!garyp From: garyp@cognos.UUCP (Gary Puckering) Newsgroups: comp.lang.perl Subject: Re: Printing Numbers with commas in them Keywords: commas formatting Message-ID: <9300@cognos.UUCP> Date: 7 Feb 91 15:08:43 GMT References: <389@cti1.UUCP> Reply-To: garyp@cognos.UUCP (Gary Puckering) Organization: Cognos Inc., Ottawa, Canada Lines: 31 In article <389@cti1.UUCP> kmeek@cti1.UUCP (Kevin Meek) writes: >Is there an easy way to print numbers formatted with commas that I have >missed in the manuals? > >If not does anyone have a subroutine that will do it in an effective manner? Picking up on Tom Neff's recent posting, I wrote this for one of my programs: sub dollar { local($_) = sprintf("%.2f",$_[0]); 1 while s/(^[^.]*\d)(\d{3})/$1,$2/; $_; } This makes it easy to do something like: $amount = 12458.62; printf ( "Amount = %16s\n",&dollar($amount)); which prints: Amount = 12,458.62 -- Gary Puckering Cognos Incorporated VOICE: (613) 738-1338 x6100 P.O. Box 9707 UUCP: uunet!mitel!cunews!cognos!garyp Ottawa, Ontario INET: garyp%cognos.uucp@uunet.uu.net CANADA K1G 3Z4