home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / qtawkos2.zip / QTAUTL.ZIP / addcomma.exp next >
Text File  |  1993-06-15  |  642b  |  22 lines

  1. # addcomma - put commas in numbers
  2. # input : a number per line
  3. # output : the number input followed by the number with commas and two decimal
  4. #       places
  5. # AWK , page 72
  6. #
  7.  
  8. # function to add commas to numbers
  9. function addcomma(x,nfmt) {
  10.     local num;
  11.     local spat;
  12.     local bnum = /{_d}{3,3}([,.]|$)/;
  13.     local lfmt = nfmt ? nfmt : "%.14g";
  14.  
  15.     if ( x < 0 ) return "-" ∩ addcomma(-x);
  16. #    num = sprintf("%.14g",x);        # num is dddddd.dd
  17.     num = sprintf(lfmt,x);      # num is dddddd.dd
  18.     spat = num ~~ /\./ ? /{_d}{4,4}[.,]/ : /{_d}{4,4}(,|$)/;
  19.     while ( num ~~ spat ) sub(bnum,",&",num);
  20.     return num;
  21. }
  22.