PDA

View Full Version : C - How do I print numbers with commas to the screen?


bobleny
03-12-2010, 07:59 AM
I have an integer that might look something like this:
int num = 1000000;

I would like to be able to print it to the screen formatted like this:
1,000,000

Is there a way to do this without changing my integer into a character and then adding commas?

Thanks.

bobleny
03-13-2010, 02:56 AM
I'm assuming that a lack of response means that there is no magical way of placing commas within a number. As a result, I have decided to try to make a string with the commas...

Well, I'm stuck. I can't figure out how to get the comma to stay in my string. It is there one second, and them gone the next... ???

This is what I came up with:

void FormatNumber(double num)
{
int strLen = 100;
char str1[strLen], str2[strLen], str3[strLen];
sprintf(str1, "%lf", num);
char *dot = strchr(str1, '.');
int i, count;

for(i = 0; i < (dot - str1); ++i)
{
strcpy(&str2[i], &str1[i]);
}
strcpy(&str2[i], "\0");
printf("%s\n", str2);


if(strlen(str2) > 3)
{
strcpy(&str3[strLen], "\0");

count = 0;
for(i = (strlen(str2) - 1); i >= 0; --i)
{
--strLen;
if(count == 3)
{
strcpy(&str3[strLen], ",");
printf("%d: %c\n", strLen, str3[strLen]);
count = 0;
--strLen;
}

strcpy(&str3[strLen], &str2[i]);
printf("%d: %c\n", strLen, str3[strLen]);

++count;
}
printf("%s\n", &str3[strLen]);
}
}


Where num equals 1000, I get this print out:

1000
99: 0
98: 0
97: 0
96: ,
95: 1
1000


Messa no understand...

Could someone help me out please?

bobleny
03-13-2010, 02:27 PM
Seriously, no one figured this out!?
Sheesh, and you call your selfs experts... lol

It is because I used strcpy().
It took me awhile to figure this out, but this is what was happening:

char str2[100] = "1000";
char str3[100];

strcpy(&str3[100], "\0");
// str3[100] now equals \0
// If printed, you would get nothing

strcpy(&str3[99], &str2[3]);
// str3[99] now equals 0
// str3[100] still equals \0
// If printed, you would see: 0

strcpy(&str3[98], &str2[2]);
// str3[98] now equals 0
// str3[99] still equals 0
// str3[100] still equals \0
// If printed, you would see: 00

strcpy(&str3[97], &str2[1]);
// str3[97] now equals 0
// str3[98] still equals 0
// str3[99] still equals 0
// str3[100] still equals \0
// If printed, you would see: 000

strcpy(&str3[96], ",");
// str3[96] now equals ,
// str3[97] now equals \0
// str3[98] still equals 0
// str3[99] still equals 0
// str3[100] still equals \0
// If printed, you would see: ,

strcpy(&str3[95], &str2[0]);
// str3[95] now equals 1
// str3[96] now equals 0
// str3[97] now equals 0
// str3[98] still equals 0
// str3[99] now equals \0
// str3[100] still equals \0
// If printed, you would see: 1000

What made it confusing, was that for a moment, the comma was there...

For some reason I was thinking because they are strings, I had to use strcpy in C, but because I am using the characters of a string, I can do this:

void FormatNumber(double num)
{
int strLen = 100;
char str1[strLen], str2[strLen], str3[strLen];
sprintf(str1, "%lf", num);
char *dot = strchr(str1, '.');
int i, count;

for(i = 0; i < (dot - str1); ++i)
{
str2[i] = str1[i];
}
str2[i] = '\0';
printf("%s\n", str2);

if(strlen(str2) > 3)
{
str3[strLen] = '\0';

count = 0;
for(i = (strlen(str2) - 1); i >= 0; --i)
{
--strLen;
if(count == 3)
{
str3[strLen] = ',';
count = 0;
--strLen;
}

str3[strLen] = str2[i];

++count;
}
printf("%s\n", &str3[strLen]);
}
}


The output of this, of course, is:

1000
1,000


This is normally when I thank someone for helping me out, but.. All well. lol