Is this how to format numeric strings? 
Author Message
 Is this how to format numeric strings?

#!/bin/ksh
#
#       This script will format a numeric string argument
#       with the conventional comma every three digits from
#       the right at the thousands, millions, billions, ...,
#       mathematical places.
#
#       Copyright Dighera Data Services Sat Oct 28 01:11:26 PDT 1995
#

NUM=$1
print $NUM|
awk '{  h=length("'"$NUM"'")                 # number of numerals in number
        k=1                                  # index into format array j
        q=0                                  # current numeral position
        for (i=h;i>0;i--) {j[k]=substr($0,i,1);q=q+1 # fill format array
           if (q==3 && i>1) {k=k+1;j[k]=",";q=0}     # insert a comma here
           k=k+1                                # prepare for next numeral
        }                    
        for (i=k-1;i>0;i--) {printf("%s",j[i])       # formatted output
        }
        print ""                                     # print trailing newline
     }'
--
USPS: The Consultants' Exchange, PO Box 12100, Santa Ana, CA  92712

TELE: (714) 842-6348; (714) 842-5851: Public guest & bbs logins
UUCP: conexch Any ACU 9600 17148426348 ogin:-""-ogin:-""-ogin: nuucp



Wed, 15 Apr 1998 03:00:00 GMT  
 Is this how to format numeric strings?
: NUM=$1
: print $NUM|
: awk '{  h=length("'"$NUM"'")                 # number of numerals in number
:         k=1                                  # index into format array j
:         q=0                                  # current numeral position
:         for (i=h;i>0;i--) {j[k]=substr($0,i,1);q=q+1 # fill format array
:            if (q==3 && i>1) {k=k+1;j[k]=",";q=0}     # insert a comma here
:            k=k+1                              # prepare for next numeral
:         }                    
:         for (i=k-1;i>0;i--) {printf("%s",j[i])       # formatted output
:         }
:         print ""                                     # print trailing newline
:      }'

There's always more than one way to skin a cat :-) ...

echo $NUM | awk '
{
        # While there's a sequence of 4 chars before a comma or the
        # end of string, stick a comma between chars 1 & 2 of that sequence
        while( match( $1, /[0-9]...,|[0-9]...$/ ) ) {
                $1 = substr($1, 1, RSTART) "," substr($1, RSTART+1)
        }
        print $1

Quote:
}'

---



Wed, 15 Apr 1998 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Picture Tokens - Numeric to String Format

2. format numeric into string loses decimal?

3. format converting string to numeric

4. format specifiers in format string param.

5. Format numeric picture at runtime?

6. Numeric Decimal Format ???

7. numeric format

8. conversion functions for Microsoft Basic 4 and 8 byte numeric format

9. Time duration numeric format

10. numeric format problem

11. Numeric format

12. g77 bug: numeric error in formatted output

 

 
Powered by phpBB® Forum Software