How to display 1234567 as 1,234,567 ? 
Author Message
 How to display 1234567 as 1,234,567 ?

As far as I know, this is a pretty safe, non-OS dependent question...

Is there a format specifier to display an unsigned long int (say, 1234567)
with commas (like 1,234,567) ?

Using "%lu" in printf() gives the first, but I don't know if such an animal
exists for the second.

The application is a unix-like "du" I've written for DOS, but the specific
problem seems independent of the OS (IMO).

So far, the only thing I've found to be close (but ugly) is some obnoxious
and expensive math manipulation and partitioning using mods, etc.  Heaven
forbid I might have to convert the value to a string and parse it... ick.

Any suggestions would be appreciated.

Paul

---
    __________
 _ /\  _______\ ____________________________________________________________
|  \ \ \____  /  Paul Smith,   Elect. Analysis |     Time is something that |
|   \ \ \/ / /  GM Truck Engineering,  Troy MI |   prevents everything from |

|_____\  / /___________________________________|____________________________|
       \/_/



Mon, 19 May 1997 22:53:20 GMT  
 How to display 1234567 as 1,234,567 ?
|> As far as I know, this is a pretty safe, non-OS dependent question...
|>
|> Is there a format specifier to display an unsigned long int (say, 1234567)
|> with commas (like 1,234,567) ?
|>

Use sprintf and putc.

Cheers
Tanmoy

--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87544-0285,USA H:#3,802,9 St,NM87545

<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Mon, 19 May 1997 23:59:10 GMT  
 How to display 1234567 as 1,234,567 ?

Quote:

>As far as I know, this is a pretty safe, non-OS dependent question...
>Is there a format specifier to display an unsigned long int (say, 1234567)
>with commas (like 1,234,567) ?
>Using "%lu" in printf() gives the first, but I don't know if such an animal
>exists for the second.
>The application is a unix-like "du" I've written for DOS, but the specific
>problem seems independent of the OS (IMO).
>So far, the only thing I've found to be close (but ugly) is some obnoxious
>and expensive math manipulation and partitioning using mods, etc.  Heaven
>forbid I might have to convert the value to a string and parse it... ick.
>Any suggestions would be appreciated.
>Paul
>---
>    __________
> _ /\  _______\ ____________________________________________________________
>|  \ \ \____  /  Paul Smith,   Elect. Analysis |     Time is something that |
>|   \ \ \/ / /  GM Truck Engineering,  Troy MI |   prevents everything from |

>|_____\  / /___________________________________|____________________________|

        \/_/
Paul,
    Try program below, it may needs some modificatios.
Regards
Habib

#include <stdio.h>
main()
{
     int i;
     char *str,*str2,*f_str,*f_str2;
     char *tostr();
     printf("->");
     scanf("%d",&i);
     str= tostr(i);
     str2 = (char *)malloc(100 * (sizeof(char)));
     if(str2 == NULL)
           exit(1);    /* Error */
     to_rev(str);
     for(f_str = str,f_str2 = str2; *(str); ){
         for(i = 0; i < 3 && *(str) ; i++,str2++,str++)
             *(str2) = *(str);
         if(! (*(str)) ) break;
         *(str2) = ',';
         str2++;
     }/*for*/
     str2 = f_str2;
     to_rev(str2);
     printf("%s\n",str2);
     exit(0);

Quote:
}

to_rev(str)
char *str;
{
    char *rev_str,*f_str,*f_rev_str;
    rev_str = (char *)malloc(strlen(str)+1);
    if(rev_str == NULL)
        return(0);
    f_str = str;
    f_rev_str = rev_str;
    for(str += strlen(str) -1;str >= f_str; str--,rev_str++)
        *(rev_str) = *(str);
    ++rev_str;
    *(rev_str) = '\0';
    strcpy(f_str,f_rev_str);
    free(f_rev_str);
    return(1);
Quote:
}

char *tostr(l)
int l;
{
   char p[40];
   return(sprintf(p,"%d",l));

- Show quoted text -

Quote:
}



Tue, 20 May 1997 09:27:03 GMT  
 How to display 1234567 as 1,234,567 ?


Quote:
> to_rev(str)
> char *str;
> {
>     char *rev_str,*f_str,*f_rev_str;
>     rev_str = (char *)malloc(strlen(str)+1);
>     if(rev_str == NULL)
>         return(0);
>     f_str = str;
>     f_rev_str = rev_str;
>     for(str += strlen(str) -1;str >= f_str; str--,rev_str++)
>         *(rev_str) = *(str);
>     ++rev_str;
>     *(rev_str) = '\0';
>     strcpy(f_str,f_rev_str);
>     free(f_rev_str);
>     return(1);
> }

If your compiler's library doesn't include a strrev(), here's a more
efficient solution from SNIPPETS:

/*
**  STRREV.C - reverse a string in place
**
**  public domain by Bob Stout
*/

#include <string.h>

char *strrev(char *str)
{
      char *p1, *p2;

      if (! str || ! *str)
            return str;
      for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
      {
            *p1 ^= *p2;
            *p2 ^= *p1;
            *p1 ^= *p2;
      }
      return str;

Quote:
}

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Tue, 20 May 1997 16:28:51 GMT  
 How to display 1234567 as 1,234,567 ?
There's no standard format specifier for including commas in numerical
values in standard C, so you have to generate a string. If you're looking
for sample code, there's Commafmt.C in SNIPPETS.

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Wed, 21 May 1997 05:22:42 GMT  
 How to display 1234567 as 1,234,567 ?

Quote:

> >            *p1 ^= *p2;
> >            *p2 ^= *p1;
> >            *p1 ^= *p2;

> This is supposed to be *efficient*??

Of course temporary registers are almost always faster. The overall
algorithm is efficient but the only claim for xor swapping is that it's
clever. Perhaps I should include both with conditional compilation based
on a selection of EFFICIENT or CUTE macros...

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Wed, 21 May 1997 05:28:09 GMT  
 How to display 1234567 as 1,234,567 ?

Quote:
> and expensive math manipulation and partitioning using mods, etc.  Heaven
> forbid I might have to convert the value to a string and parse it... ick.

    What do you mean heaven forbid? This is fairly easy...

        char    *comma(dp,val) /*===================================*/
        char    *dp;
        double  val;
        {
        char    *cp;
        int     i,len;
        char    tmp0[20],tmp1[20];
            len = sprintf(tmp0,"%.0f",val);
            tmp1[19] = 0;
            cp = tmp1 + 19;
            for (i = 0; i < len; i++) {
                if (i && !(i % 3)) *--cp = ',';
                *--cp = tmp0[len - (i + 1)];
            }
            strcpy(dp,cp);
            return(dp);
        }

        #ifdef TEST
        int     main(argc,argv) /*==================================*/
        int     argc;
        char    *argv[];
        {
        int     i;
        char    buf[20];
            for (i = 1; i < argc; i++) printf("%s\n",comma(buf,atof(argv[i])));
            exit(0);
        }
        #endif

    Note that this function has no error checking on the size or
    validity of the parameters it is being passed, nor is any attempt
    made to use localisation features to substitute a '.' or whatever
    for a comma in countries that do that sort of thing.

Quote:
> Paul

--

***             Count Templar, ELITE, Cobra Mk III (FM-287)             ***


Wed, 21 May 1997 06:19:28 GMT  
 How to display 1234567 as 1,234,567 ?

: -------------------------------------------------------------
: MicroFirm: Down to the C in chips...
: Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
: FidoNet 1:106/2000.6

Can you please tell me if SNIPPETS archives are held on a ftp site?
Only having just started to sit down and take the time to learn C PROPERLY,
any tips or code examples could be of great benefit.



Wed, 21 May 1997 07:25:50 GMT  
 How to display 1234567 as 1,234,567 ?


Quote:
> Can you please tell me if SNIPPETS archives are held on a ftp site?
> Only having just started to sit down and take the time to learn C PROPERLY,
> any tips or code examples could be of great benefit.

oak.oakland.edu (or other Simtel mirrors) in /pub/msdos/c/snip9404.zip

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Thu, 22 May 1997 09:01:43 GMT  
 How to display 1234567 as 1,234,567 ?

|> [Quote from another post deleted.]

|> If your compiler's library doesn't include a strrev(), here's a more
|> efficient solution from SNIPPETS:
|> [...]
|> #include <string.h>
|> [...]
|> char *strrev(char *str)
|> [Efficient- and correct-looking code deleted.]

The code looks fine but the function name could cause trouble.

(You probably know this, but for the newbies ...) ISO 7.13.8 (String
handling<string.h>) warns:
        Function names that begin with str, mem or wcs and a lowercase
        letter (followed by any combination of digits, letters and underscore)
        may be added to the declarations in the <string.h> header.
That is, a C implementation is allowed to provide a strrev function and to
declare it in <string.h>.  If it does, you get a compile error if the
implementor's strrev signature doesn't match yours.  More likely, your
signatures match and something bad happens at link time.

To avoid these problems, name your function something that doesn't start with
"str."  The ISO 7.13.* clauses list name patterns that could conflict with the
implementation's namespace.   There are others I haven't mentioned.

I know your strrev is meant to be used only when the local C lacks one but
what if you change compilers or upgrade?  You could easily forget to check
for strrev.  Then:  Trouble.  IMO, it's safest to have no name conflicts,
actual or potential.

--
Jim Stern -- These views are my own, not my employer's.



Tue, 03 Jun 1997 02:24:41 GMT  
 How to display 1234567 as 1,234,567 ?

Quote:


>|> [Quote from another post deleted.]

>|> If your compiler's library doesn't include a strrev(), here's a more
>|> efficient solution from SNIPPETS:
>|> [...]
>|> #include <string.h>
>|> [...]
>|> char *strrev(char *str)
>|> [Efficient- and correct-looking code deleted.]

>The code looks fine but the function name could cause trouble.

>(You probably know this, but for the newbies ...) ISO 7.13.8 (String
>handling<string.h>) warns:
>    Function names that begin with str, mem or wcs and a lowercase
>    letter (followed by any combination of digits, letters and underscore)
>    may be added to the declarations in the <string.h> header.
>That is, a C implementation is allowed to provide a strrev function and to
>declare it in <string.h>.  If it does, you get a compile error if the
>implementor's strrev signature doesn't match yours.  More likely, your
>signatures match and something bad happens at link time.

No matter what string.h contains or doesn't contain, this is a case of
undefined behaviour.

Quote:

>To avoid these problems, name your function something that doesn't start with
>"str."  

There is nothing wrong with the "str" prefix, if used correctly.  Renaming
strrev to str_rev will fix the problem.  I feel the need of a case
insensitive strcmp very often, so I implement it as str_icmp.

Quote:
>The ISO 7.13.* clauses list name patterns that could conflict with the
>implementation's namespace.  There are others I haven't mentioned.

Names starting with "to" and "is" are worth mentioning here, because
it's very easy to forget about them.

Quote:
>I know your strrev is meant to be used only when the local C lacks one but
>what if you change compilers or upgrade?  You could easily forget to check
>for strrev.  Then:  Trouble.  IMO, it's safest to have no name conflicts,
>actual or potential.

Avoiding undefined behaviour is _always_ a must, unless you never change
your compiler and you never port your code.

Dan
--
Dan Pop                       | The only reason God was able to make the
CERN, CN Division             | world in 7 days was he didn't have to remain

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 04 Jun 1997 03:34:22 GMT  
 How to display 1234567 as 1,234,567 ?

Quote:

> I know your strrev is meant to be used only when the local C lacks one but
> what if you change compilers or upgrade?  You could easily forget to check
> for strrev.  Then:  Trouble.  IMO, it's safest to have no name conflicts,
> actual or potential.

Quite correct.  In the next SNIPPETS release, I'll add a caveat comment to
the header.  Thanks for pointing this out!

-------------------------------------------------------------
MicroFirm: Down to the C in chips...
Home of SNIPPETS - Current release: SNIP9404.ZIP/LZH/ARJ/etc.
FidoNet 1:106/2000.6



Thu, 05 Jun 1997 07:05:11 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. String.Format ("{0:n}",1234567)

2. Displaying a bitmap vs. displaying its icon

3. How to display text on the foreground of my display

4. displaying RGB DIB on 256 colour display - palette?

5. Display BMP file as a panel in within a display window

6. Displaying 8bit bitmaps in 8bit display mode

7. VC 4.2: problem displaying static int variable; double value not displayed promptly;

8. Dual Display, How to draw child dialog to second display?

9. basic bitmap display question - after first display, image disappears!

10. Display a dialog box automatically after main window is displayed.

11. Q : How to have a bitmap displayed correctly whatever the display setting is small fonts or Large Fonts ?

12. How to display "Display Properties" tab from an application

 

 
Powered by phpBB® Forum Software