home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / C / SMALL_C / ITOD.C < prev    next >
Text File  |  1987-10-04  |  768b  |  25 lines

  1. #include stdio.h
  2. /*
  3. ** itod -- convert nbr to signed decimal string of width sz
  4. **         right adjusted, blank filled; returns str
  5. **
  6. **        if sz > 0 terminate with null byte
  7. **        if sz = 0 find end of string
  8. **        if sz < 0 use last byte for data
  9. */
  10. itod(nbr, str, sz)  int nbr;  char str[];  int sz;  {
  11.   char sgn;
  12.   if(nbr<0) {nbr = -nbr; sgn='-';}
  13.   else sgn=' ';
  14.   if(sz>0) str[--sz]=NULL;
  15.   else if(sz<0) sz = -sz;
  16.   else while(str[sz]!=NULL) ++sz;
  17.   while(sz) {
  18.     str[--sz]=(nbr%10+'0');
  19.     if((nbr=nbr/10)==0) break;
  20.     }
  21.   if(sz) str[--sz]=sgn;
  22.   while(sz>0) str[--sz]=' ';
  23.   return str;
  24.   }
  25.