NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Temperature Project Code explaination

December 29, 2009
by markjbenson
markjbenson's Avatar

Hi All, Just got my nerdkit for xmas - been playing a bit with the temperature project so it displays F, C & mV but my c programming is 10 years rusty so could anyone explain what the %.2f is for me?

This works...

//Also chuck data up serial port
printf_P(PSTR("%.2f degrees F "), temp_avg);
printf_P(PSTR(" or "));
printf_P(PSTR("%.2f degrees C \r\n"), temp_cent);

...but I was looking for a way to output both f & C on the serial in one line and realised I didn't understand the syntax.

I suppose I should really try this first but I have spent long enough on this thing today so will try tomorrow. Would this work?

printf_P(PSTR("%.2f degrees F or %.2f degrees C rn"), temp_avg, temp_cent);



December 29, 2009
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Mark,

Your thinking is definitely right, you can define a single format string with more than one place holder to be replaced by a formatted variable. However the problem there is going to be that you only have 20 characters in one line of the LCD. The line as you have it will overflow to a different line, which is probably not what you want.

Check out our printf and scanf tutorial for a fairly comprehensive explanation of the topic.


December 30, 2009
by markjbenson
markjbenson's Avatar

Right - the %f is the variable substitution and the .2 within that is the formatting or precision of the string (i.e. the number of trailing digits after the decimal point).

All makes sense.

I was outputting the Temperature to the serial port so wasn't worried about length.

Thanks for the tutorial - very helpful.

Post a Reply

Please log in to post a reply.

Did you know that the printf format string "%.3f" will show three digits after the decimal point? Learn more...