Thomas Stockbruegger
asked on
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
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
#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
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;
}
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Not very pretty, but ... shows the zero
011.560.600
001.120.130
000020.500
How can I fix this?
str.Format("%3d.%03d.%03d" , ((i / 1000000) % 1000), ((i / 1000) % 1000), (i % 1000));
ASKER
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(LOC ALE_USER_D EFAULT, 0, s, &numFmt, szNumber, 29) > 0);
szNumber is now 12,345.67
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(LOC
szNumber is now 12,345.67
aha - replace iDeciPlaces with 0 for no decimal places, 1 for one and so on.
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
A result of ,,123 isn't very satisfactory as a formatted string representing the number
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
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
ASKER
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
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
>> 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.
>> 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",
str.Format("%d.%03d.%03d.%
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.
ASKER
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
...okay thank you
Best regards,
Thomas
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