Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!uunet!bnrgate!bnr-fos!friedman From: friedman@chekov.UU.NET (Barry Friedman) Newsgroups: comp.lang.perl Subject: Inserting commas in numbers Message-ID: <2450@bnr-fos.UUCP> Date: 1 Mar 90 04:42:32 GMT Sender: news@bnr-fos.UUCP Reply-To: friedman@chekov.UUCP (Barry Friedman) Organization: Northern Telecom, Ottawa, Canada Lines: 33 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. ex. &commas("9999999"); returns 9,999,999 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = sub commas { local($arg) = $_[0]; local($len) = length($arg); return $arg if($len < 4); local($j, $s, $s1); local($m) = $len % 3; local($i) = $m ? 3 - $m : 0; $arg=substr(" ",0,$i).$arg; $len += $i; undef($s1); for ($j=$len;$j>0; $j=$j-3){ $s=substr($arg,-3,3); $arg=substr($arg,0,$j-3); $s1 = defined($s1) ? $s.",".$s1 : $s; } $s1 =~ s/^ *//; return $s1; } -- Barry Friedman UUCP: ...!uunet!chekov!friedman Emax Computer Systems Inc. 440 Laurier Ave. W., Ottawa, Ont. Canada K1R 5C4