Go Down

Topic: Converting number 7 to 07 (Read 747 times) previous topic - next topic


Hi all, im trying to build a lcd clock, and would like some help here.

I believe this is quite simple, i just don't know how to do it, and didn't found it how to do it with arduino.

I want to print one number, for example 7, but want it to have 2 digits, like 07.

if it was C it would be like printf("%.2d \n",x);

i'm using this

lcd.printIn(itoa(hour, buf, 10)); , want that the variable hour comes with two digits



Aug 03, 2008, 06:19 pm Last Edit: Aug 03, 2008, 06:22 pm by chippey Reason: 1
You could test the length of the resulting string, and if it's one character, print a 0 before you print the number.

This is just coding off the top of my head, so this may not work:
Code: [Select]

 int length;
 length = sizeof(itoa(hour,buf,10))/sizeof(char);
 if (length==1) lcd.printIn("0"); //assumes the number will at max only be two digits
 lcd.printIn(itoa(hour, buf, 10));

That can cleaned up some, but that's just off the top of my head.  Does that help at all?


Aug 03, 2008, 06:29 pm Last Edit: Aug 03, 2008, 06:30 pm by mem Reason: 1
this is the code I use in my clock sketches, change the serial.print as approprite for your lcd print routine:

Code: [Select]

void printDigits(byte digits){
 // utility function for digital clock display of minutes or seconds,  prints preceding colon and leading 0
 if(digits < 10)


thanks mem, it worked that way =)

Go Up