Pages: [1]   Go Down
Author Topic: Zero Padding or Leading Zeros  (Read 3024 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 2
Posts: 82
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
if (number<100) print('0');
if (number<10) print('0');
print(number;
Logged


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

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.
Logged

Southern California
Offline Offline
God Member
*****
Karma: 5
Posts: 539
I like blinky lights
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

http://en.wiktionary.org/wiki/magagna <-- My last name.  Pretty apt.

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

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

That's my choice any day of the week but I didn't know how much OP wants to learn a C function smiley-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.
Logged


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

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html, 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.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 73
Posts: 7197
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That is pretty complicated smiley-eek-blue. 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.
Logged


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

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

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 361
Posts: 17294
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)

Code:
/* 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

Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.)
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: