Pages: [1]   Go Down
Author Topic: Converting number 7 to 07  (Read 347 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

thanks!
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

  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?
« Last Edit: August 03, 2008, 11:22:12 am by chippey » Logged

London
Offline Offline
Tesla Member
***
Karma: 10
Posts: 6250
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this is the code I use in my clock sketches, change the serial.print as approprite for your lcd print routine:

Code:
void printDigits(byte digits){
  // utility function for digital clock display of minutes or seconds,  prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits,DEC);
}
« Last Edit: August 03, 2008, 11:30:57 am by mem » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 77
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks mem, it worked that way =)
Logged

Pages: [1]   Go Up
Jump to: