Fill with zeros a number?

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?

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.

I don't know if the Arduino supports it, but you can use a '*' to signify a runtime parameter:
http://c-faq.com/stdio/printfvwid.html

char numberBuffer[30];    //adjust to suit maximum length used

void setup() 
{
  Serial.begin(115200);
  for (int length = 0; length < 21; length ++)
  {
    Serial.print(length);  //debug print
    Serial.print("\t");    //debug print
    Serial.println(formatForPrint(123, length));
  }
}
void loop() 
{
}

char* formatForPrint(int number, int length)
{
  char formatBuffer[6];    //enough room for a length up to 99 characters (%099d)
  sprintf(formatBuffer,"%%0%dd",length);
  Serial.print(formatBuffer);    //debug print
  Serial.print("\t");            //debug print
  sprintf(numberBuffer, formatBuffer, number);
  return numberBuffer;
}

NOTE that numberBuffer is a global variable and not local to the formatBuffer function

NOTE that numberBuffer is a global variable and not local to the formatBuffer function

Then, there's really no reason for the function to return a pointer to it, is there? And, the function is not called formatBuffer. It's called formatForPrint().

Picky, picky (:)) but correct, of course in respect of the global variable.
I was focussing on not committing the sin of returning a pointer to a variable that was local to the function and missed the blindingly obvious.

PaulS:

NOTE that numberBuffer is a global variable and not local to the formatBuffer function

Then, there's really no reason for the function to return a pointer to it, is there? And, the function is not called formatBuffer. It's called formatForPrint().

The functions like char* itoa() does have the buffer as parameter AND return value. The added value of giving this pointer as return value is that the function itself can be used as function parameter or in other expressions e.g.
Serial.println(itoa(....));