home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / addcomma.exp next >
Text File  |  1990-01-07  |  591b  |  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.     printf("%-12s %20s\n",$0,addcomma($0));
  8.     }
  9.  
  10. # function to add commas to numbers
  11. function addcomma(x) {
  12.     local num;
  13.     local spat;
  14.     local bnum = /{_d}{3,3}([,.]|$)/;
  15.  
  16.     if ( x < 0 ) return "-" addcomma(-x);
  17.     num = sprintf("%.14g",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.