[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1598
  • Last Modified:

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
0
tsp2002
Asked:
tsp2002
  • 5
  • 4
  • 3
  • +2
2 Solutions
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now