Fill with zeros a number?

Hi. Does anybody know how can I make a function that fills with zero, some number?

Like.. I have a number that goes from 0 to 360 and I would like to do like this:

i.e:
5 would be 005;
15 would be 015;
120 would be 120;

Tks in advance..

Regards

Rodrigo

The 0s are always there. You just want them to show up when printing to something?

CrossRoads:
The 0s are always there. You just want them to show up when printing to something?

Hi. Thank you for your asnwer.

yes.. I need the number to always have 3 digits..

Doens't matter if it is a number 1 or 10 or 200.

Here's a sample...

int foo;
char buffer[4];

void setup() {
  Serial.begin(19200);
  foo = 5;
  sprintf(buffer,"%03d", foo);
  Serial.println(buffer);

}

void loop() {
  // put your main code here, to run repeatedly: 
}

The second parameter is a format string. %d is decimal, %03d is decimal with three leading zeros.

I don't know if Serial.print provides any settings for that.
Could always force it I suppose:

// time to print a number
Serial.println ("printing a 3 digit number ");
if (numberToPrint <=9){
Serial.print("00");
Serial.println(numberToPrint);
}
if ((numberToPrint>=10) && (numberToPrint <=99)){
Serial.print("0");
Serial.println(numberToPrint);
}

if (numberToPrint>99){
Serial.println(numberToPrint);
}

lar3ry:
Here's a sample...

int foo;

char buffer[4];

void setup() {
  Serial.begin(19200);
  foo = 5;
  sprintf(buffer,"%03d", foo);
  Serial.println(buffer);

}

void loop() {
  // put your main code here, to run repeatedly:
}




The second parameter is a format string. %d is decimal, %03d is decimal with three leading zeros.

Hi, Iar3ry. Thank you very much.

On Setup function it worked 100%.

I just need to now find out hot to use it as a function and uses that function to return the formated number

Thank you very much

CrossRoads:
I don't know if Serial.print provides any settings for that.
Could always force it I suppose:

// time to print a number

Serial.println ("printing a 3 digit number ");
if (numberToPrint <=9){
Serial.print("00");
Serial.println(numberToPrint);
}
if ((numberToPrint>=10) && (numberToPrint <=99)){
Serial.print("0");
Serial.println(numberToPrint);
}

if (numberToPrint>99){
Serial.println(numberToPrint);
}

Hi.. That could be an solution but I need to use this code a sa funcion so I can pass values and get the correct values returned.

@Crossroads,
Serial.print does not support this, for this the print class should be rewritten.

char buffer[4];

void setup() 
{
  Serial.begin(115200);
  Serial.println(formatForPrint(1));
  Serial.println(formatForPrint(10));
  Serial.println(formatForPrint(100));
}

void loop() 
{
}

char* formatForPrint(int foo)
{
  sprintf(buffer,"%03d", foo);
  return buffer; 
}

UKHeliBob:

char buffer[4];

void setup()
{
  Serial.begin(115200);
  Serial.println(formatForPrint(1));
  Serial.println(formatForPrint(10));
  Serial.println(formatForPrint(100));
}

void loop()
{
}

char* formatForPrint(int foo)
{
  sprintf(buffer,"%03d", foo);
  return buffer;
}

Thank you very much for your help. it worked.

Could you please give me some site reference about the "%03d" and what is each parameter is for?

I will apreciatte and learn more.

Thank you.

cabecinhas:
Could you please give me some site reference about the "%03d" and what is each parameter is for?

http://lmgtfy.com/?q=sprintf+reference

The code works for numbers within the rang 0..999

try the code with a number that is longer or a negative and you see some point to improve the function.

http://www.cplusplus.com/reference/cstdio/printf/

Warning: The arduino does not support all the type specifiers, specifically floating point %f and %g.

Does everything from C++ works on arduino?

If you can't afford the memory overhead of "sprintf"

if (numberToPrint < 100) {
  Serial.print("0");
}
if (numberToPrint < 10) {
  Serial.print("0");
}
Serial.print(numberToPrint);

robtillaart:
The code works for numbers within the rang 0..999

try the code with a number that is longer or a negative and you see some point to improve the function.

Quite right. I had meant to point that out in my reply.

In practice I would probably not use such a function in my code as it only replaces two lines with one in the main code and I would feel more in control of the formatting by having the sprintf on the line before the print instead of hidden in a function. It would also become more and more difficult to extend the function to handle other formats properly.

@UKHeliBob
It was more meant as an exercise for the OP, not as a remark on your code sec.
I've seen enough of your code to know you would implement it as you said :wink:

Don't worry. No offence taken.

Hi... Is there a way to make the "number of zeros" sent as a parameter?

I made the "total_casas " as a parameter but didn't work.

I'm new in C++. Maybe it seems to be abvious but not for me :frowning: l

char* formata_zeros_esquerda(int foo, String total_casas)
{

char buffer_formata_zeros_esquerda[4];
sprintf(buffer_formata_zeros_esquerda,"%0" + total_casas +"d", foo);
return buffer_formata_zeros_esquerda;
}

This one works but only for "3 zeros"
char* formata_zeros_esquerda(int foo, String total_casas)
{
char buffer_formata_zeros_esquerda[4];
sprintf(buffer_formata_zeros_esquerda,"%03d", foo);
return buffer_formata_zeros_esquerda;
}

thank you very much

This one works but only for "3 zeros"

char* formata_zeros_esquerda(int foo, String total_casas)
{
    char buffer_formata_zeros_esquerda[4];
  sprintf(buffer_formata_zeros_esquerda,"%03d", foo);
  return buffer_formata_zeros_esquerda;
}

It doesn't work at all well for any number of zeroes.
Never return a pointer to an automatic.

However, with two "sprintf"s (the first for the format string), you could very easily support any number of zeroes.