1

Topic: prefix a leading zero to a char variable

I want to display the following data with a leading zero if the value is less than 10.

  value = minutes_run; 
  dtostrf(value, 2, 0, RemoteXY.minutes_run);  
  value = seconds_run; 
  dtostrf(value, 2, 0, RemoteXY.seconds_run);  

I'm having a mind-melt on how best to do it, I know it should be straightforward enough surely ...

Can anyone help, I'm running out of time on this project ....

2B, or not 2B, that is the pencil ...

2

Re: prefix a leading zero to a char variable

I suppose I should have said char[] array variable, rather than just a single char.

But never mind, I've decided to not display the seconds and minutes of my run_time, just the hours, so the problem (for now) has gone away.

I'm certain I'll be able to figure it out if I need to do it in the future, but it would sure be nice if there was a time display library, not found one yet.

2B, or not 2B, that is the pencil ...

3

Re: prefix a leading zero to a char variable

For example

  void writeTime () {
    uint32_t d = millis();
    long ds = d/1000;
    long dm = d%1000;
    char s[15];
    sprintf (s, "[%5ld.%03ld] ",ds, dm);       
    serial->println ();    
    serial->print (s);
  }

4

Re: prefix a leading zero to a char variable

remotexy wrote:

For example

  void writeTime () {
    uint32_t d = millis();
    long ds = d/1000;
    long dm = d%1000;
    char s[15];
    sprintf (s, "[%5ld.%03ld] ",ds, dm);       
    serial->println ();    
    serial->print (s);
  }

Very good, but that doesn't help me to add leading zeroes to the RemoteXY structure members of my original post ...

To be honest, I get confused between char[] and string variables, and when to use, and when not to use, I guess I need to watch some good tutorials...

2B, or not 2B, that is the pencil ...

5

Re: prefix a leading zero to a char variable

struct {
    // output variables
  char minutes_run[4];  // string UTF8 end zero 
  char seconds_run[4];  // string UTF8 end zero 
} RemoteXY;

sprintf (RemoteXY.minutes_run, "%02d", minutes_run);
sprintf (RemoteXY.seconds_run, "%02d", seconds_run);

minutes_run and seconds_run is a text string

6

Re: prefix a leading zero to a char variable

remotexy wrote:
struct {
    // output variables
  char minutes_run[4];  // string UTF8 end zero 
  char seconds_run[4];  // string UTF8 end zero 
} RemoteXY;

sprintf (RemoteXY.minutes_run, "%02d", minutes_run);
sprintf (RemoteXY.seconds_run, "%02d", seconds_run);

minutes_run and seconds_run is a text string

Thank you : working well ....

2B, or not 2B, that is the pencil ...