Solved

Number formatting

Posted on 2003-11-02
6
464 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
Comment Utility
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
Comment Utility
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
Comment Utility
(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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:njitram
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you, got it working now. :)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now