From owner-freebsd-questions Fri Feb 2 4: 6:13 2001 Delivered-To: freebsd-questions@freebsd.org Received: from shasta.wstein.com (rfx-64-6-196-149.users.reflexcom.com [64.6.196.149]) by hub.freebsd.org (Postfix) with ESMTP id 4806437B491 for ; Fri, 2 Feb 2001 04:05:55 -0800 (PST) Received: from hood (hood.wstein.com [192.168.250.14]) by shasta.wstein.com (8.11.1/8.11.1) with ESMTP id f12C5s931277 for ; Fri, 2 Feb 2001 04:05:54 -0800 (PST) (envelope-from joes@joescanner.com) Date: Fri, 2 Feb 2001 04:05:54 -0800 (Pacific Standard Time) From: Joseph Stein To: Subject: Formatting a number Message-ID: X-X-Sender: joes@joescanner.com MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG A few weeks ago, I posted a question asking how to format a number. I couldn't find a solution (however I am still certain there is an easier way than what I finally hacked out)... Here was the solution I came up with in case anybody else wants it. It is pretty rudimentary, but it works. /* * fmtnbr.c * * Input: a number (string) * Output: A formatted number * * Copyright (C) 2001, Joseph Stein (joes@joescanner.com) * * Permisson to use this code is granted to anybody. If you * include it in other code, please give me credit. */ #include int main(int argc, char* argv[]) { int numlen; int i; int j; int remainder; char teststr[256]; if (argc!=2) { fprintf(stderr,"Usage:\n\t%s \n\n", argv[0]); return(2); } strcpy(teststr, argv[1]); numlen=strlen(teststr); teststr[numlen]='\0'; remainder=numlen%3; #ifdef DEBUG fprintf(stderr,"number %s is %d digits.\n", teststr, numlen); #endif if (numlen<4) { printf("%s", teststr); exit(1); } if (remainder==0) { #ifdef DEBUG fprintf(stderr,"multiple of 3\n"); #endif for (i=0; i