ESP Format to align decimal points

I an Arduino novice and I am trying to display the output of an AMS2320 temperature sensor on an M5StickC LCD.
I would like the decimal point to remain in a fixed position with one decimal place for all temperatures between -40 and +80 degrees C.
Right justify would work but I can't see how to do it.
I eventually came up with the format below which works but seems a but I'm not sure why.
The width defined by * and 5 seem to apply the number only and exclude the 'C.

M5.Lcd.printf("TEMP %*.1f'C",5 , t);

Could somebody explain it to me please.

Please study the printf format specifiers and examples on the C/C++ reference pages.

The format specifier is a C-string, and therefore has to be completely enclosed by double quotes.

What is the "5" supposed to do? In the posted code, the "5" is used as the value to be formatted and printed according to the format specifier string.

Read about the printf() functions, along with dtostrf(()

The * in the format string specifies that the field width is given as a argument, in this case a field width of 5 with 1 decimal place. The OP could have written a format string of "TEMP %5.1f'C" instead. Presumably the character before the C could be a degree symbol.

Thanks, I had forgotten about the '*' width specifier.

Thanks, I'm happier with that.
I missed that the 5.1f includes the dp anddecimal places, I had tried 3.1f which of course didn't work.

Yes the ' is an attempt to represent the degree symbol.

The '*' variable field width option does not appear to be supported for AVR Arduinos, and of course, neither is "f" for float/double arguments.

These lines work as expected on a Pro Mini:

char buf[12];
int t=25;
sprintf(buf,"TEMP %5dC",t);
Serial.println(buf); //prints TEMP     25C

but these do not

char buf[12];
int t=25;
sprintf(buf,"TEMP %*dC",5,t);
Serial.println(buf); //prints only TEMP 

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.