Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Number formatting

Posted on 2003-11-02
6
Medium Priority
?
525 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
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.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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 …
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

596 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