Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!giza.cis.ohio-state.edu!jgreely From: jgreely@giza.cis.ohio-state.edu (J Greely) Newsgroups: comp.lang.perl Subject: Re: Inserting commas in numbers Message-ID: Date: 1 Mar 90 07:05:39 GMT References: <2450@bnr-fos.UUCP> Sender: news@tut.cis.ohio-state.edu Reply-To: J Greely Organization: Ohio State University Computer and Information Science Lines: 22 In-reply-to: friedman@chekov.UU.NET's message of 1 Mar 90 04:42:32 GMT In article <2450@bnr-fos.UUCP> friedman@chekov.UU.NET (Barry Friedman) writes: >After getting tired of looking at 10 digit+ numbers in a report, >I came up with the following routine to insert commas. >Unfortunately, I couldn't figure out how to use split to split groups >of three so this is kind of long. Hope you find it useful. You might like this one, which is a bit shorter, and also handles floating point numbers. It's not bulletproof, but it works. sub commas { local($_,$t1,$t2,$s)=@_; ($_,$s) = /^([0-9]*)(\.[0-9]*)?$/; while () { ($t1,$t2) = /^(.*)(...)$/; last unless $t2; $s = ",$t2$s"; $_ = $t1; } $_ ? $_ . $s : substr($s,1,length($s)); } -- J Greely (jgreely@cis.ohio-state.edu; osu-cis!jgreely)