Number of digits to print

So this is probably a very simple question to anwer for you guys, but I haven't been able to find an answer to it. Is there a way to print more digits of an integer, let's say I have an integer A = 5 and want to print it's value like "0005", instead of just "5", is there a simple way to do choose how may digits should be printed?

sprintf lets you specify the width of the field and whether or not it has leading zeroes.
e.g."%05d"

AWOL:
sprintf lets you specify the width of the field and whether or not it has leading zeroes.
e.g."%05d"

I'm sorry, but I'm not very accustomed to the different commands, I'll try to illustrate below with an example:

Let's say I want to make a counter that counts from 0 to whatever number, using the following sketch:

//start
int A = 0;

void setup(){
Serial.begin(9600);
}

void loop(){
Serial.println(A);
A++;
delay(1000);
}

This sketch will print like this: 0, 1, 2, ....
How do I implement the sprintf command in order to make it display the numbers like this: 0000, 0001, 0002... ?

For a quick into to sprintf(), take a peek at:

Additional details at:

Okay, so this can’t be done without using a library?

You could write a function to do it yourself, but that kinda seems like reinventing the wheel. What's your concern?

econjack:
You could write a function to do it yourself, but that kinda seems like reinventing the wheel. What’s your concern?

It’s just that I’m trying to get an understanding of things, but the sprintf-function will do just fine. Thanks for helping! :). I guess it can be used with other formats as well float for instance?

Not floating point no. The Arduino implementation of sprintf and associated functions disables the floating point code to save space.

sprintf is part of the C/C++ core libraries, so its common between most (all?) environments which use C++. It is possible to do this another way, but it looks far more messy:

if (A < 10) {
 Serial.write('0');
}
if (A < 100) {
 Serial.write('0');
}
if (A < 1000) {
 Serial.write('0');
}
Serial.println(A);

I see. I was thinking about something like that but suspected there had to be an easier way. Thank you, sir! :wink:

char temp[6];
sprintf(temp, "%04d", A);
Serial.println(temp);

There is still the possibility to enable float support for sprintf (and sscanf), see this post:

http://forum.arduino.cc/index.php?topic=124809.msg954691#msg954691

majenko:

char temp[6];

sprintf(temp, "%04d", A);
Serial.println(temp);

Thanks, but out of curiousity; why do you define "temp" as having 6 characters initially?

Lars81:

majenko:

char temp[6];

sprintf(temp, "%04d", A);
Serial.println(temp);

Thanks, but out of curiousity; why do you define "temp" as having 6 characters initially?

4 for the digits, one for the string end "NULL" character, and one extra for luck as an int can be 5 digits long. In actual fact, 7 would be a better choice, for a signed integer, which can be be between -32768 and 32767. For an unsigned integer it's 0 to 65535, so 6 characters is good.

Even better safety would be to use snprintf instead of sprintf, which will limit the number of characters it can try and put into the resultant string (snprintf(temp, 5, "%04d", A):wink:

For a unsigned integer it's 0 to 65535, so 6 characters is good

AWOL:

For a unsigned integer it's 0 to 65535, so 6 characters is good

Oops, typo there :wink: corrected. Good eyes there :stuck_out_tongue:

majenko:
Even better safety would be to use snprintf instead of sprintf, which will limit the number of characters it can try and put into the resultant string (snprintf(temp, 5, "%04d", A):wink:

Even better would be to have the size argument be the actual size of the array, and not a hard-coded value that may or may not be correct.

let's say I have an integer A = 5 and want to print it's value like "0005", instead of just "5", is there a simple way to do choose how may digits should be printed?

I'd change the number 5 to the character 5 and then add the number of desired character 0's to the left of the 5 character.

if you are happy to modify your copy of print.cpp &print.h then go see this thread.
in particular the last couple of lines of the second bit of code posted is what you would need

http://forum.arduino.cc/index.php?action=post;quote=1333896;topic=179111.30;num_replies=37;sesc=b62ba7d0009032290aff5c37a65ef8b8