|
|
View previous topic :: View next topic |
Author |
Message |
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
Display comma separated value in fprintf [DONE] |
Posted: Tue Oct 13, 2020 1:54 pm |
|
|
Compiler: 5.026
Device: PIC24EP512GP806
Hi guys,
Quick question: I have an int64 variable and I am currently printing it as-is on a serial monitor like this example:
int64 CardSize = 3931151360;
fprintf( MONITOR_SERIAL, "{%Ld bytes}", CardSize );
This works, output is {3931151360 bytes}.
Is there a a way to get the output to have the commas like this:
{3,931,151,360 bytes}
Thanks.
Ben
Last edited by benoitstjean on Wed Oct 14, 2020 12:59 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Tue Oct 13, 2020 2:36 pm |
|
|
Quick answer...
Convert to a string, then insert commas 'somehow'.
I'll try something later....too nice down here near Hamilton to be INSIDE.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Wed Oct 14, 2020 12:11 am |
|
|
You don't say whether you need to handle signed values?.
However this (for +ve values), should return a string containing the
digits with commas added.
Code: |
char *outputcommas(int64 n)
{
static char retbuf[30];
char *p = &retbuf[sizeof(retbuf)-1];
int i = 0;
*p = '\0';
do {
if(i%3 == 0 && i != 0)
*--p = ',';
*--p = '0' + n % 10;
n /= 10;
i++;
} while(n != 0);
return p;
}
|
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Wed Oct 14, 2020 4:49 am |
|
|
I assumed they're all +ve numbers based on the variable named 'CardSize'
hmm, my brute force method I did last night didn't show up ! grr....
also have NO hot water in the house either, GRRRRRRRR...
that's TWO things gone wrong so far and the sun's not up yet.
Jay
4th try...
Code: |
unsigned int32 x=4294967294;
char mystring[20];
char nustring[20]= "0,123,456,789";
lcd_gotoxy(1,2);
printf(lcd_putc,"test %Lu",x);
sprintf(mystring,"%Lu",x);
lcd_gotoxy(1,3);
printf(lcd_putc,mystring);
nustring[0]=mystring[0];
nustring[1]=',';
nustring[2]=mystring[1];
nustring[3]=mystring[2];
nustring[4]=mystring[3];
nustring[5]=',';
nustring[6]=mystring[4];
nustring[7]=mystring[5];
nustring[8]=mystring[6];
nustring[9]=',';
nustring[10]=mystring[7];
nustring[11]=mystring[8];
nustring[12]=mystring[9];
lcd_gotoxy(1,4);
printf(lcd_putc,nustring);
|
|
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Oct 14, 2020 5:05 am |
|
|
ok, thanks both of you.
Have a nice day.
Ben |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|