Solved

Number formatting

Posted on 2003-11-02
6
496 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Handle Exceptions during instantiation 28 423
Unable to start eclipse ? 17 154
max float value 3 51
C++ help/ Toy problem 19 30
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

831 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