DHT.h + 1602 display - Remove decimals

Hey there !

I have an issue displaying temperature and humidity.
I'd like to display only the entire number on the LCD but there's always the decimals.
i.e.: 22.70
I'd like to have only 22

Here's the display function :

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//
  void lcd_display_temp(){
    
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float f1 = dht1.readTemperature(true);
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  float f2 = dht2.readTemperature(true);
  
  lcd.setCursor(0,1);
  lcd.print("I");
  lcd.print(t1);
  lcd.print("/");
  lcd.print("H");
  lcd.print(h1);
  lcd.print("E");
  lcd.print(t2);
  lcd.print("/");
  lcd.print("H");
  lcd.print(h2);
  }
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//  

Here's the whole code :

#include <Wire.h>

#include <RTC.h>
static DS1307 RTC;

#include <SPI.h>
#include <SD.h>

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);

#include "DHT.h"
#define dht_int A1
#define dht_ext A2
#define DHTTYPE DHT11
DHT dht1(dht_int, DHTTYPE);
DHT dht2(dht_ext, DHTTYPE);

//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//
  File myFile;
  
  void save_temperature() {
  myFile = SD.open("temp.txt", FILE_WRITE);
  
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float f1 = dht1.readTemperature(true);
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  float f2 = dht2.readTemperature(true);
  
  if (RTC.getDay()<10){
  myFile.print(0);
  }
  myFile.print(RTC.getDay());
  myFile.print("-");
  if (RTC.getMonth()<10){
  myFile.print(0);
  }
  myFile.print(RTC.getMonth());
  myFile.print(",");
  if (RTC.getHours()<10){
  myFile.print(0);
  }
  myFile.print(RTC.getHours());
  myFile.print(":");
  if (RTC.getMinutes()<10){
  myFile.print(0);
  }
  myFile.print(RTC.getMinutes());
  myFile.print(",");
  myFile.print("T+H_INT:,");
  myFile.print(t1);
  myFile.print(",");
  myFile.print(h1);
  myFile.print(",");
  myFile.print("T+H_EXT:,");
  myFile.print(t2);
  myFile.print(",");
  myFile.println(h2);
  myFile.close();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//
  void lcd_display_temp(){
    
  float h1 = dht1.readHumidity();
  float t1 = dht1.readTemperature();
  float f1 = dht1.readTemperature(true);
  float h2 = dht2.readHumidity();
  float t2 = dht2.readTemperature();
  float f2 = dht2.readTemperature(true);
  
  lcd.setCursor(0,1);
  lcd.print("I");
  lcd.print(t1);
  lcd.print("/");
  lcd.print("H");
  lcd.print(h1);
  lcd.print("E");
  lcd.print(t2);
  lcd.print("/");
  lcd.print("H");
  lcd.print(h2);
  }
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//  
  void lcd_display_time() {
      
  lcd.setCursor(0,0);
  if (RTC.getDay()<10){
  lcd.print(0);
  }
  lcd.print(RTC.getDay());
  lcd.print("-");
  if (RTC.getMonth()<10){
  lcd.print(0);
  } 
  lcd.print(RTC.getMonth());
  lcd.print("/");
  if (RTC.getHours()<10){
  lcd.print(0);
  }
  lcd.print(RTC.getHours());
  lcd.print(":");
  if (RTC.getMinutes()<10){
  lcd.print(0);
  }
  lcd.print(RTC.getMinutes());
  }
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//
void setup()
{
  Serial.begin(112500);
  RTC.begin();
  Wire.begin();
  dht1.begin();
  dht2.begin();
  SD.begin(10);
  delay(100);
  lcd.init();
  lcd.backlight();
}
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*//
void loop() 
{
  save_temperature();
  lcd_display_time();
  lcd_display_temp();

  delay(1000);
}

Thanks in advance for the help !!!

Also available for download :
NANO_LCD_I2C_1602_RTC_DHT11_SDCARD_Recording_2_DHT11_every_minu.ino (2.7 KB)

When printing floats there is an extra argument that specifies the number of decimal places to print.
It defaults to 2 places without the extra argument.

float pi = 3.14159;
lcd.print(pi);  // prints 3.14
lcd.print(pi, 4); // prints 3.1416 (corrected, see reply #3)
lcd.print(pi, 0)  // prints 3

Or make the temperature and humidity the int data type.
lcd.print(int(t1));

1 Like

print(), used with a float as a parameter, will show 2 decimal digits by default. You can pass the number of digits you want to print as an extra parameter ➜
lcd.print(t1, 0); // print with 0 decimal digit

Edit: weird, I did not see tge existing answer until I posted mine, sorry @groundFungus for just repeating the same thing

Side note:

That will actually print 3.1416 because it’s rounding, not truncating

1 Like

Thanks. Corrected.

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