How to get rid of the decimals after calulation when displayed on the lcd?

Hi, I am fairly new to coding and I am hoping I can get some help with this problem. I am trying to display the temperature in both C and F however after some calculations there seems to be many numbers after the demical showing up on the lcd that I would like to get rid of.
Here is a part of my code, let me know if you need more:

#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 8, 9, 10, 11);

float TX0, TX1, TX2, TX3, TX4, TX5, TX6, TX7, TX8, TX9, TXavg, T0, rh, ed, Td, ew, Tw;

void setup() {
lcd.begin(16,2);
}

void loop() {
TXavg = ((TX0+TX1+TX2+TX3+TX4+TX5+TX6)/7);
ed = 6.112pow(2.71828,((17.502Td)/(240.97+Td)));
ew = 6.112pow(2.71828,((17.502Tw)/(240.97+Tw)));
rh = ((ew-0.6687451584*(1+0.00115Tw)(Td-Tw))/ed)*100;

lcd.setCursor(0,0);
lcd.scrollDisplayLeft();
lcd.print("Temp Avg (C): ");
lcd.print(TXavg = ((TX0+TX1+TX2+TX3+TX4+TX5+TX6)/7));
Serial.print("TXavg (C): ");

lcd.setCursor(0,1);
lcd.print("Temp Avg (F): ");
lcd.print((TXavg * 9)/5 + 32);
Serial.print("TXavg (F): ");

lcd.setCursor(16,0);
lcd.print(" Humidity (%): ");
lcd.print(rh = ((ew-0.6687451584*(1+0.00115Tw)(Td-Tw))/ed)*100);
Serial.print("Humidity: ");
delay(500);
}

Erase the line prior to positioning the cursor.

If you simply assign the variable in question to an int variable, it will be truncated to the nearest integer value, so everything right of the decimal point disappers. 123.456 become simple 123.

float x = 123.456;
Serial.println(x);
int y = x;
Serial.println(y);

Your code appears messed up on the screen. Please type ``` on a line by itself before your code, and again after your code.

To round to the nearest integer use...

  Serial.println((int)(TXavg + 0.5));

That is one way to deal with the limitations of Arduino print.
Really, it is sad that leading zeros and leading spaces are "left as an exercise for the reader".

Not quite. It will be truncated to the next lower integer: 3.999 becomes 3

If you use lcd.print(value) on a 'float' or 'double' expression, it will be shown with 2 digits after the decimal point. If you don't want the decimal part, use:
lcd.print(value, 0)
The second argument to print(), when printing a 'float' or 'double', tells print() how many digits to show after the decimal point.

Exactly what I said - the float will be truncated. i.e. - rounded DOWN. Exactly the same as simply making everything right of the decimal point 0. If he wants it rounded UP, he needs to do that explicitly.

You also said, "to the NEAREST integer value". Certainly, 3.999 is nearer to 4 than it is to 3. That is why I clarified that it WASN'T "to the NEAREST integer value". If you had left that part out I would not have been confused.

Thank you. I have tried all of these suggestions but I still get decimals when I display the values on the lcd. Also, can some explain what "Erase the line prior to positioning the cursor" means? This is the only one I have yet to try.

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