Solved

Number formatting

Posted on 2003-11-02
6
488 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
  • 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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 50 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ syntax question 9 49
Arduino EDI - Programming language 3 95
Add values of each row in an array 3 58
Embarcadero WebBroker REST server endpoint parameters 2 69
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

832 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