SOLVED ! is there a way of putting preceding '0's in a print?

If i have

if (seconds<10)
  lcd.setCursor(7,0);
  else
  lcd.setCursor(6,0);
  lcd.print(seconds);

I can get a two figure number to show up right but is there any way of just putting
lcd.print (seconds,‘something’) so that it gives me leading '0’s?

You want 04 for example. Print a 0 if the number is less than 10, next print the number.

Try

printf("%02d",seconds);

Lcd.print(seconds);

I think you can get rid of the seconds condition with this code as well.

Yep, use the “print2digits” approach as below:

#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>

void setup() {
  Serial.begin(9600);
  while (!Serial) ; // wait for serial
  delay(200);
  Serial.println("DS1307RTC Read Test");
  Serial.println("-------------------");
}

void loop() {
  tmElements_t tm;

  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);
}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

mistergreen,
I altered my program to include

 lcd.setCursor(6,0);
  printf("%02d",seconds);
  lcd.print(seconds);

but I still get 1-9 on line 7 and then 10-59 on lines 7 and 8. What I wanted was 00-09 to show on lines 7 and 8 and then 10-59 on line 7 and 8.
jimboZA, I don’t need the date and I am printing to a 1 line lcd so I will see if I can alter your program soon, thanks to both of you for the replies.

if (seconds<10)
{
  lcd.setCursor(6,0);
  lcd.print('0');
  lcd.setCursor(7,0);
}
  else
{
  lcd.setCursor(6,0);
}
  lcd.print(seconds);

yea LarryD if you read my question that is almost what I have and in fact since I wrote it I have added the '0's but I was asking if there is any way to tell the program to print the leading '0's by saying 'give me a two figure number' similar to mistergreens %02d. I am just learning and wonder if there is a way of doing that.

Hi matelot.

Take a look at this:

sprintf(buffer, “%02d:%02d:%02d”, hour, min, sec);
lcd.print(buffer); //< You will get “13:02:00”

See this for parameters (float will not work though)
http://www.cplusplus.com/reference/cstdio/printf/
.

thanks LarryD, I read through your link and found an error that you may see straight away but I know little about arrays at this point so had no idea what was wrong. I know a lot more about arrays now the line

char [9] buffer = "";

brings up

expected unqualified-id before '[' token

when compiled but

char buffer[9] = "";

works fine and I think is a neater way than the way I was doing. Thanks.

Congrats! You are an expert now.

do I get a badge :confused: ......or something?