Print out to the serial monitor

Is there a way to format the numbers that you send out to the serial monitor.
So instead of the following:

Motor Output = 55
Motor Output = 105

you would get:

Motor Output = 55
Motor Output = 105

or

Motor Output = 055
Motor Output = 105

Pass them through sprintf:

int mynum = 39;
char temp[5];

Serial.print("Number: ");
sprintf(temp,"%03d",mynum);
Serial.println(temp);
Number: 039

thx

Found this after some searching

float testFloat = 10.24;

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

void loop()
{
  char tBuffer[16];
  
  Serial.println(dtostrf(testFloat,8,2,tBuffer)); 
  delay(1000);
  testFloat = testFloat + 1.1;  
}

Yep, sprintf doesn't have the float code (it's massive). dtostrf() is your friend in this case.

In dire straits I have been known to write:

    // print three digit val, zero-filled
    if (val < 100) Serial.print('0');
    if (val < 10) Serial.print('0');
    Serial.print(val);

-br

billroy:
In dire straits I have been known to write:

    // print three digit val, zero-filled

if (val < 100) Serial.print('0');
    if (val < 10) Serial.print('0');
    Serial.print(val);




-br

Perfectly legitimate, and nice and light and fast. Good for keeping the code small, and clean.

You could also generalise it:

void zprint(unsigned int value, unsigned int zeros)
{
    unsigned long pad = 1;

    while(zeros>0)
    {
        pad = pad * 10;
        zeros--;
    }
 
    while(pad>value)
    {
        Serial.print("0");
        pad = pad / 10;
    }

    Serial.print(value);
}