?
Solved

Number formatting

Posted on 2003-11-02
6
Medium Priority
?
517 Views
Last Modified: 2008-03-17
Hello,

I'm working on a program that counts certain things, and I want to display the current counts in a readable notation like 1,000 and 4,500, I've been looking on howto do this, but have had absolutely no luck. And since the fact that I'm not that good with C++, I've probaly been looking for the wrong thing. =\

Ok, Here's what it's going to be used for;

DDX_Text(pDX, IDC_SOMEIDC, Number_Format_Function(counts));

counts is an UINT.

It's being used in MFC, but the function would be pure C++, so I posted it here.

I'd love any pointers, or even examples, I'm stuck on this.

Thanks.
0
Comment
Question by:njitram
[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
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:bcladd
ID: 9665886
XFormatNumber seems to do what you want, http://www.codeproject.com/string/xformatnumber.asp (free registration required for download)
CodeGuru also has a long2string function, http://www.codeguru.com/string/long2string.html

Hope this helps, -bcl

0
 

Author Comment

by:njitram
ID: 9666209
I have tried this,  however the program keeps crashing because of an Access violation, using:

DDX_Text(pDX, IDC_SOMEIDC, XFormatNumber((LPCTSTR)count, -1));
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9666294
(1) See if you can get your problem in a small program, one that you could post and I could run.

(2) Alternative: Assign the result of XFormatNumber to a variable so you can easily look at it in the debugger. Running under the debugger you can determine where the error occurs (in XFormatNumber or after...I don't know the function except by notation).

-bcl
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:njitram
ID: 9666430
Creating a new MFC project, and adding this:

CString XFormatNumber(LPCTSTR lpszNumber, int nFracDigits)
{
          ASSERT(lpszNumber);
          ASSERT(lpszNumber[0] != 0);
      
          CString str = lpszNumber;

          NUMBERFMT nf;
          memset(&nf, 0, sizeof(nf));

          _TCHAR szBuffer[10];
          _TCHAR szDecSep[10];
          _TCHAR szThousandsSep[10];

          // set decimal separator string from locale default
          GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL,
                     szDecSep, sizeof(szDecSep)-1);
           nf.lpDecimalSep = szDecSep;

           // set thousand separator string from locale default
           GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND,
                       szThousandsSep, sizeof(szThousandsSep)-1);
            nf.lpThousandSep = szThousandsSep;

            // set leading zeroes from locale default
            GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ILZERO,
                    szBuffer, sizeof(szBuffer)-1);
             nf.LeadingZero = _ttoi(szBuffer);

           // set grouping of digits from locale default
           GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SGROUPING,
                      szBuffer, sizeof(szBuffer)-1);
            nf.Grouping = _ttoi(szBuffer);

             // set negative number mode from locale default
             GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_INEGNUMBER,
                  szBuffer, sizeof(szBuffer)-1);
                   nf.NegativeOrder = _ttoi(szBuffer);

     int dp = nFracDigits;

     if (dp == -1)
     {
             // set number of fractional digits from input string
             CString strDecSep = szDecSep;
             dp = str.Find(szDecSep);
             dp = (dp == -1) ? 0 : str.GetLength() - dp - strDecSep.GetLength();
      }
      nf.NumDigits = dp;

      int nSize = (_tcslen(lpszNumber) * 3) + 100;
      _TCHAR *pszFormattedNumber = new _TCHAR [nSize];
      ASSERT(pszFormattedNumber);

      if (GetNumberFormat(LOCALE_USER_DEFAULT,
           0,
           lpszNumber,
           &nf,
            pszFormattedNumber,
            nSize-1))
     {
           str = pszFormattedNumber;
     }

     if (pszFormattedNumber)
     {
           delete [] pszFormattedNumber;
     }
      return str;
}

Somewhere in the beginning of the program:

UINT SomeThing = 23425;
      
AfxMessageBox(XFormatNumber((LPCTSTR)SomeThing, -1));

Assigning the result of XFormatNumber does not work either, results into the same (Access violation).
0
 
LVL 11

Accepted Solution

by:
bcladd earned 150 total points
ID: 9666586
Yep, that would cause a problem. XFormatNumber expects a STRING (or rather a LPCTSTR or a far pointer at a cstr) as its first parameter. You cast an integer to a pointer (the compiler trust you) and that causes a serious problem. It is unlikely that location 23425 contains a string.

So, first convert to a C-style string:

UINT Something = 23425;
char conversionBuffer[26]; // can convert 25 digit numbers with this buffer.

itoa(Something, conversionBuffer); // int to ascii; make sure you include stdlib.h or cstdlib (same thing but one requires std::itoa for name of the funciton)
AfxMessageBox(XFormatNumber((LPCTSTR)conversionBuffer, -1));

Didn't compile this myself but it is the right direction.

-bcl
0
 

Author Comment

by:njitram
ID: 9666662
Thank you, got it working now. :)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

752 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