16x2 LCD formatting question

I tried to include the full code as I've seen many before me reprimanded for not doing the same, but it exceeded the message limitation (9000), and I didn't know what to include or not include.

You could have attached the whole program or provided a small but complete example of the problem.

mattress67: So this is a bit screwy using this 16x2 LCD. I want to display the temperature in the following format (for example): 78.35 degrees.

I’m using a Dallas One Wire thingy - and I get good data using the serial monitor. For example, when I open the serial monitor, I get the following: 78.35. Perfect.

Using the 16x2 display, when I use the code: lcd.print(temperature), I get: 78

Now, I realize, that for the 16x2 display, I have to put my own “.” between the first two (or three) digits, followed by a lcd.print(“.”) and then the last two digits.

My question, is how do I get the just the last two digits from my variable “temperature”?

I envision the code to be something like this:

 lcd.print("Temp is: ");
lcd.print(temperature); // just need the last two digits of the variable here

and a little degree symbol would be nice too!


Sorry, your question cannot be answered unless you post your entire code and provide your height, weight , and shoe size and how old is your grandmother...

Try this

float fResult = (float) 22/(float) 7; lcd_i2c.clear(); lcd_i2c.println((int) fResult);

lcd_i2c.println((float) fResult,32);

lcd_i2c.println((float) fResult,32);

2 questions: Why on earth is it necessary to cast a float to a float? On what planet does it make sense to print a float that has 6 or 7 digits of precision to 32 decimal places?

i use this

lcd.print(celsius, DEC); // Temp xx lcd.print((char)223); // Degree Symbol

Obviously not as an int, long, byte,
(you tested the effect of using ints by getting the .00).

Read my snippet (it works) what does it take as parameter?
Take a guess. :wink:

Well, since you used the abs() function, by the process of elimination, I guessed that I should have defined my variable “temperature” as a char. But that didn’t work either. So, I’m not sure why I can’t get a good result using a variable.

To others, Whandall nailed it :slight_smile: and the following code is working:

int val = (sensors.getTempFByIndex(0)) * 100; //Originally I tried int val = temperature * 100 but as I said, nogo.
int dec = val % 100;

    lcd.print(val / 100);
    if (abs(dec) < 10) {

and for my 16x2 board, the degree symbol is lcd.print("\337"), not the 228 (just a chip set thing).


If it wasn’t obvious, defining the variable temperature as a “float” works too - but it wasn’t in the table!!!

mattress67: and for my 16x2 board, the degree symbol is lcd.print("\337"), not the 228 (just a chip set thing).

which 228?

"\337" is a string of one character with the value 223 (dezimal) 0337 (octal) 0xDF (hex)

the octal form is very rarly use.

Sorry, I meant 223 didn't work. Two of the three choices that worked when it came to the degree symbol were:



lcd.print((char)223); worked as well

lcd.printByte(0xDF) gives me "class LiquidCyrstal" has no printByte.

Maybe I use a slightly different library. you could try:

// lcd.printByte(0xDF);


Hi, If your sketch is too big, use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your sketch as an attachment, but try code tags first.

Tom..... :)

mattress67: AAAgggh!!!

If it wasn't obvious, defining the variable temperature as a "float" works too - but it wasn't in the table!!!

Well @mattress67, looks like you got battered a bit; the forum can get pretty aggressive about the full-code request. May I suggest that the next time you hit a rough patch to start a new sketch and just concentrate on the specific problem.... 10 or 15 lines usually is enough to reproduce an issue.

Second, as you may have noticed, variables of type float are often poo-poo'd because the library code to manipulate floating numbers is expensive: both on increasing SRAM and flash requirements and also in CPU cycles. (Just means floats make code slower.) My take on this is that if you use floats and you are pleased with the results, then all is well. However, if you use floats and your program runs too slowly, you are in for a long night of editing!

IMO, unless you write code for a living, just do what you want. But know, the pros on the forum are attempting to impart 'best practices' but some of the suggestions may not make a lots of immediate sense.

Ray My Stuff

The "Arrggg" was directed towards myself, so thanks to all. The advise has been great and I'm getting ready to post another elusive matter.