Solved

Print a number with comma delimited

Posted on 2006-07-02
11
605 Views
Last Modified: 2012-06-27
I want to use printf to print out a number, say 4325932032, with comma delimited in thousands, that is, 4,325,932,032.

There is a way on an online faq telling us to use locale or a code snippet. We just wonder if there is a more reliable and convinient way to do so?
0
Comment
Question by:chcw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 22

Expert Comment

by:mahesh1402
ID: 17035682
following function accepts in form of long integer and returns comma delimited character array

const char * ConvertCD(long v)
{
  static char   buf[16];
  static char   buf2[16];
  int           i;
  int           j;
  int           k;

  if (v >= 0)
    sprintf (buf, "%d", v);
  else
    sprintf (buf, "%d", -v);
  if ((k = i = strlen (buf)) == 0)
    return buf;
  i = (i-1) / 3;
  for (j = 0; j < i; j++)
    { sprintf (buf2, "%.*s,%s", k - 3*(j+1), buf, buf + k - 3*(j+1));
    strcpy (buf, buf2);
    }
  if (v < 0)
    {
    sprintf (buf2, "-%s", buf);
    return buf2;
    }
  else
    return buf;
}

-MAHESH
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17041636
Hi there,

I would take the number apart by powers of ten, i.e. 10, 100, 1000
but only look at powers dividable by 3, i.e. 1,1000, 1000000, ...

const char *ConvertCD(long Num)
{
bool IsNeg = (Num < 0);
char strNum[32]={""};      //** stores the result
long p(1), Frac;            //** Frac stores one three digits fraction of number
char buf[4];            //** temp. buffer for _itoa()

Num = abs(Num);                           // work on positive values
strcat(strNum, Num < 0 ? "-": "+");   // prefix sign
while (p)
{
      p      = 3*((int)log10(Num)/3);
      Frac  =  Num/pow(10,p);                           // contains 3-digit number (* 10^9, 10^6, 10^3, 10^0)
      Num -= Frac*pow(10,p);                          

      strcat(strNum, _itoa(Frac, buf, 10));      // this is not ansi .. maybe use sprinf(buf, "%i,", Frac);
      if (p>0) strcat(strNum, ",");
}
return strNum;
}
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17042128
There is an API function that will do that - GetNumberFormat

GetNumberFormat
The GetNumberFormat function formats a number string as a number string customized for a specified locale.

int GetNumberFormat(
  LCID Locale,                // locale
  DWORD dwFlags,              // options
  LPCTSTR lpValue,            // input number string
  CONST NUMBERFMT *lpFormat,  // formatting information
  LPTSTR lpNumberStr,         // output buffer
  int cchNumber               // size of output buffer
);

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:bastibartel
ID: 17042136
*darn*
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17042169
;-)
0
 
LVL 8

Author Comment

by:chcw
ID: 17047787
hi, AndyAinscow,

Can you provide a sample of usage? I cannot find one in MSDN.
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 17048543
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 17208198
My suggestion uses a standard windows function to do what was requested.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question