CString format need some help

Hello,
I need some help with format.
I have a long int like 11500600 and I woul like to convert the int to a CString.

int i= 11500600;
CString str;

str.Format("%d",i);
so str is now 11500600
I would like to change the look  to 11.500.600 or 11,500,600 that you can see the 11 mio easier.
Is there any way with the format function?
I don´t want to use this....
if(str>999999)            
{
      str.Insert(4,".");
     str.Insert(1,".");
}...and so on
Thomas StockbrueggerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
       str.Format("%03d.%03d.%03d", ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));

Not very pretty, but ...
0
AndyAinscowFreelance programmer / ConsultantCommented:
GetNumberFormat
The GetNumberFormat function formats a number string as a number string customized for a specified locale.



That does what you want - see the help for more details
0
alb66Commented:
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Deepu AbrahamR & D Engineering ManagerCommented:

#include <stdio.h>
#include <stdlib.h>
 
int main ()
{
  long int li;
  char szInput [256];
  printf ("Enter a long number: ");
  gets ( szInput );
  li = atol (szInput);
  printf ("The value entered is %d. The double is %d.\n",i,i*2);
  return 0;
}
 
You could convert this to CString

Open in new window

0
Deepu AbrahamR & D Engineering ManagerCommented:
Sorry I got it wrong you could try this: :P
#include <stdio.h>
#include <stdlib.h>
 
int main ()
{
  char szNumbers[] = "2001 60c0c0 -1101110100110100100000 0x6fffff";
  char * pEnd;
  long int li1, li2, li3, li4;
  li1 = strtol (szNumbers,&pEnd,10);
  li2 = strtol (pEnd,&pEnd,16);
  li3 = strtol (pEnd,&pEnd,2);
  li4 = strtol (pEnd,NULL,0);
  printf ("The decimal equivalents are: %ld, %ld, %ld and %ld.\n", li1, li2, li3, li4);
  return 0;
}

Open in new window

0
Thomas StockbrueggerAuthor Commented:
GetNumberFormat
The GetNumberFormat function formats a number string as a number string customized for a specified locale.

That does what you want - see the help for more details....

Can you give me a example with my 11500600 int ?
I looked it up at msdn...but I need a example to understand, thanks.
0
AndyAinscowFreelance programmer / ConsultantCommented:
NUMBERFMT
The NUMBERFMT structure contains information that defines the format of a number string. The GetNumberFormat function uses this information to customize a number string for a specified locale.

typedef struct _numberfmt {
  UINT      NumDigits;
  UINT      LeadingZero;
  UINT      Grouping;
  LPTSTR    lpDecimalSep;
  LPTSTR    lpThousandSep;
  UINT      NegativeOrder;
} NUMBERFMT, *LPNUMBERFMT;
Members
NumDigits
Specifies the number of fractional digits. This is equivalent to the locale information specified by the LCTYPE constant value LOCALE_IDIGITS.
LeadingZero
Specifies whether to use leading zeroes in decimal fields. This is equivalent to the locale information specified by the LCTYPE constant value LOCALE_ILZERO.
Grouping
Specifies the size of each group of digits to the left of the decimal. Values in the range 0 9 and 32 are valid. For example, grouping by thousands is indicated by 3, while the Indic grouping of the first thousand and then by hundreds is indicated by 32that is, three digits, then two-digit groupings thereafter.
lpDecimalSep
Pointer to a null-terminated decimal separator string.
lpThousandSep
Pointer to a null-terminated thousand separator string.
NegativeOrder
Specifies the negative number mode. This is equivalent to the locale information specified by the LCTYPE constant value LOCALE_INEGNUMBER.




int GetNumberFormat(
  LCID Locale,                // locale
  DWORD dwFlags,              // options
  LPCTSTR lpValue,            // input number string
  CONST NUMBERFMT *lpFormat,  // formatting information
  LPTSTR lpNumberStr,         // output buffer
  int cchNumber               // size of output buffer
);
Parameters
Locale
[in] Specifies the locale for which the number string is to be formatted. If lpFormat is NULL, the function formats the string according to the number format for this locale. If lpFormat is not NULL, the function uses the locale only for formatting information not specified in the NUMBERFMT structure (for example, the locale's string value for the negative sign).
This parameter can be a locale identifier created by the MAKELCID macro, or one of the following predefined values. Value Meaning
LOCALE_SYSTEM_DEFAULT Default system locale.
LOCALE_USER_DEFAULT Default user locale.



dwFlags
[in] Controls the operation of the function. If lpFormat is non-NULL, this parameter must be zero.
If lpFormat is NULL, you can specify LOCALE_NOUSEROVERRIDE to format the string using the system default number format for the specified locale; or you can specify zero to format the string using any user overrides to the locale's default number format.

lpValue
[in] Pointer to a null-terminated string containing the number string to format.
This string can only contain the following characters:

Characters '0' through '9'.
One decimal point (dot) if the number is a floating-point value.
A minus sign in the first character position if the number is a negative value.
All other characters are invalid. The function returns an error if the string pointed to by lpValue deviates from these rules.

lpFormat
[in] Pointer to a NUMBERFMT structure that contains number formatting information. All members in the structure pointed to by lpFormat must contain appropriate values.
If lpFormat is NULL, the function uses the number format of the specified locale.

lpNumberStr
[out] Pointer to a buffer that receives the formatted number string.
cchNumber
[in] Specifies the size, in TCHARs, of the lpNumberStr buffer. If cchNumber is zero, the function returns the number of TCHARs required to hold the formatted number string, and the buffer pointed to by lpNumberStr is not used.





      NUMBERFMT numFmt;

      numFmt.NumDigits = iDeciPlaces;
      numFmt.LeadingZero = true;
      numFmt.NegativeOrder = 1;
      numFmt.Grouping = 3;
      TCHAR chDecimal[] = _T(".");
      numFmt.lpDecimalSep = &chDecimal[0];
      TCHAR chGroup[] = _T(",");
      numFmt.lpThousandSep = &chGroup[0];

      CString s;
      s.Format(_T("%f"), 123245.67);   s = "12345.67"

      TCHAR szNumber[30];
      VERIFY(GetNumberFormat(LOCALE_USER_DEFAULT, 0, s, &numFmt, szNumber, 29) > 0);
szNumber is now 12,345.67
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Thomas StockbrueggerAuthor Commented:
    str.Format("%03d.%03d.%03d", ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));

Not very pretty, but ... shows the zero
011.560.600
001.120.130
000020.500
How can I fix this?
0
alb66Commented:
str.Format("%3d.%03d.%03d", ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));
0
Thomas StockbrueggerAuthor Commented:
StatistikDialog.cpp(886) : error C2065: 'iDeciPlaces': nichtdeklarierter Bezeichner
My compiler shows this error???
please help

NUMBERFMT numFmt;

      numFmt.NumDigits = iDeciPlaces;
      numFmt.LeadingZero = true;
      numFmt.NegativeOrder = 1;
      numFmt.Grouping = 3;
      TCHAR chDecimal[] = _T(".");
      numFmt.lpDecimalSep = &chDecimal[0];
      TCHAR chGroup[] = _T(",");
      numFmt.lpThousandSep = &chGroup[0];

      CString s;
      s.Format(_T("%f"), 123245.67);   s = "12345.67"

      TCHAR szNumber[30];
      VERIFY(GetNumberFormat(LOCALE_USER_DEFAULT, 0, s, &numFmt, szNumber, 29) > 0);
szNumber is now 12,345.67
0
AndyAinscowFreelance programmer / ConsultantCommented:
aha - replace iDeciPlaces with 0 for no decimal places, 1 for one and so on.
0
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  Test all proposed solutions with numbers like 123 not just with 12345678.
A result of ,,123 isn't very satisfactory as a formatted string representing the number
0
alb66Commented:
Try the following routine:

CString FormatX( int nNumber )
{
      CString str, str1;
      
      while ( 1 )
      {
            if ( nNumber < 1000 )
            {
                  str1.Format( "%d", nNumber );
                  str = str1 + str;
                  break;
            }
            else
            {
                  int n = nNumber % 1000;
                  str1.Format( "%03d", n );
                  str = "." + str1 + str;
                  nNumber /= 1000;
            }
      }

      return str;
}

You can test it with this code:

      TRACE( FormatX( 1 ) + "\n" );
      TRACE( FormatX( 12 ) + "\n" );
      TRACE( FormatX( 123 ) + "\n" );
      TRACE( FormatX( 1234 ) + "\n" );
      TRACE( FormatX( 12345 ) + "\n" );
      TRACE( FormatX( 123456 ) + "\n" );
      TRACE( FormatX( 1234567 ) + "\n" );
      TRACE( FormatX( 12345678 ) + "\n" );
      TRACE( FormatX( 123456789 ) + "\n" );
      TRACE( FormatX( 1234567890 ) + "\n" );

And the output will be:

1
12
123
1.234
12.345
123.456
1.234.567
12.345.678
123.456.789
1.234.567.890


0
Thomas StockbrueggerAuthor Commented:
ps.  Test all proposed solutions with numbers like 123 not just with 12345678.
A result of ,,123 isn't very satisfactory as a formatted string representing the number
I did...from 1 to 100.000.000 everything works fine.
Thanks a lot.
Best regards,
Thomas
0
Infinity08Commented:
>> Not very pretty, but ... shows the zero
>> 011.560.600
>> 001.120.130
>> 000020.500
>> How can I fix this?

The leading 0's can be fixed by using %d instead of %03d for the first one like alb66 showed.

When the value is smaller than 1.000.000 or larger than 999.999.999, then you'll have to use a different Format string, resp. :

        str.Format("%d", (i % 1000));
        str.Format("%d.%03d", ((i / 1000) % 1000), (i % 1000));
        str.Format("%d.%03d.%03d", ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));
        str.Format("%d.%03d.%03d.%03d", ((i / 1000000000) % 1000), ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));

Note that these will only work correctly for positive ints too ... For negative ints, a bit more code needs to be added.

Doing it this way quickly becomes ugly, so only consider this if you know the length and range of the ints you want to output. If they can differ, then choose a different approach.
0
Thomas StockbrueggerAuthor Commented:
Doing it this way quickly becomes ugly, so only consider this if you know the length and range of the ints you want to output. If they can differ, then choose a different approach.
...okay thank you
Best regards,
Thomas
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.