CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Display comma separated value in fprintf [DONE]

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

Display comma separated value in fprintf [DONE]
PostPosted: Tue Oct 13, 2020 1:54 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Oct 13, 2020 2:36 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 14, 2020 12:11 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 14, 2020 4:49 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Oct 14, 2020 5:05 am     Reply with quote

ok, thanks both of you.

Have a nice day.

Ben
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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