Zero Padding or Leading Zeros

How do you zero pad an integer in the Arduino IDE? Say X can be 0 to 100, and I want to display X as a 3 digit number i.e. 1 = 001.

1 Like

Where do you want the number displayed? On serial monitor, LCD?
Below is pseudo code. You need to replace the print with whatever output device you use.

if (number<100) print('0');
if (number<10) print('0');
print(number;

Sorry about that, the serial monitor and a LCD. So the only way to do that is with a if statement, there isn't a built in way to do it? I am working with two number and at times one might be greater than 100 and the other might be less than 100.

printf("%03d", number);

If you need to send it to both the serial port and a LCD, use sprintf() to print it to a string, then print that string to both outputs.

Thanks Chris, sprintf() might be the way to go because I am also using the NewSoftSerial library.

That's my choice any day of the week but I didn't know how much OP wants to learn a C function :wink:

I visit this site every time I forget how to use all the nice features of sprintf:

http://www.cplusplus.com/reference/clibrary/cstdio/sprintf/

If you do "%3d" then you get space padding to the left.

1 Like

Thanks for the link liudr. Is sprintf library dependant, I didn't get a chance to test it out last night?

With sprintf in the blink example code, it compiles just fine without using any library. I think the library is already included by arduino by default.

liudr:
With sprintf in the blink example code, it compiles just fine without using any library. I think the library is already included by arduino by default.

That is correct, though in a somewhat circuitous manner.

main.cpp include WProgram.h (the sketch you create is automatically merged with main.cpp by the Arduino IDE)
WProgram includes from the AVR Libc library:
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html
http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html
http://www.nongnu.org/avr-libc/user-manual/group__avr__math.html

But also includes from the 'Arduino library', among other things, HardwareSerial.h
HardwareSerial.h includes Stream.h
Stream.h includes Print.h
And Print.h includes avr-libc: <stdio.h>: Standard IO facilities, which provides the various versions of printf.

It's all rather academic from the perspective of being able to use sprintf(), but it never hurts to have an understanding of what's going on beneath the covers, and I think it's always useful to know that the variety of C library functions in those modules are available for your usage within Arduino sketches by default.

1 Like

That is pretty complicated :fearful:. I can certain appreciate a breakdown of what happens and hope the OP gets it too.

I have been including stdio.h but I'll remove it now that a version of it is already included.

I totally undestand. Thanks for the breakdown jraskell. I'm going to try sprintf out when I get home.

and I think it's always useful to know that the variety of C library functions in those modules are available for your usage within Arduino sketches by default.

Could be useful to have a list of functions avalible that don't require an include directive?

Lefty

Here is example code for using sprintf() to print formatted integers and dtostrf() for floats that works:
(not very elegant, but then neither is C, esp for printing.)

/* Print padded (aligned) integers and floats from Arduino environment  
 *  sprintf() for integers          www.cplusplus.com/reference/cstdio/printf/
 *  and dtostrf() for floats        dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER); gist.github.com/2343665   www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#g6c140bdd3b9bd740a1490137317caa44
 */

int n;
int m;
byte i = 1;
float x;
char c[10];        // long enough to hold complete integer string
char f[10];        // long enough to hold complete floating string

void setup() {      
  randomSeed(1);  
  Serial.begin(57600); 
}

void loop() {
  n = random(-1000,1000);                    // generate a random integer
  x = n/1000.0;                              // convert to a float <= |1.0|

  m = sprintf(c, " %3d:  %5d    ", i,n);    // build integer string using C integer formatters  (m is length, and not used in this code)
  Serial.print(c);   

  dtostrf(x,6,3,f);                          // -n.nnn Use this for a consistent float format
  Serial.println(f); 
  i++;
  delay(1000);               
}

Output:

   1:   -193    -0.193
   2:    249     0.249
   3:   -927    -0.927
   4:    658     0.658
   5:    -70    -0.070
   6:    272     0.272
   7:    544     0.544
   8:   -122    -0.122
   9:    923     0.923
  10:    709     0.709
  11:   -560    -0.560
  12:   -835    -0.835

retrolefty:
Could be useful to have a list of functions avalible that don't require an include directive?

That might be a moving target - I don't suppose the Arduino development team consider that the set of implicitly-included headers for any given library or for the environment as a whole is parts of the defined interface for that library or environment. (Microsoft don't either; they're always changing things around and breaking code that took the implicit includes for granted.)