Not getting expected result using %4d

I just noticed @MartyMacGyver in his code is using

snprintf(printbuf, sizeof(printbuf), "PM1.0:%4d", currFrame.concPM1_0_amb);

but when I try to use the same with sprintf i still dont get expected result to 2 decimal point!


sprintf(buff, "%4d", thisFrame->concPM1_0_amb);
Particle.publish("PM 1.0 (μg/m3)", buff, 40, PRIVATE);

i still see this:
Screen Shot 2020-04-25 at 22.35.08

in the web console.

What are you expecting to get from the format specifier %4d? It takes an int and if it has less than 4 digits it right-justifies it with spaces (42 --> “__42”).

What I was publishing was a long list of data in a single line. If you want this in columns like that, I’d just use %d since it’s not really necessary to bother indenting it visually as I did.

(And as the value is an int, not a float, there’s no point to having any decimal places for it.)

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