Getting rid of decimals

How to I get rid of decimals? I have an IoT project with Blynk that has 3 decimal places. I want humidity and pressure to have no decimals and temperature to have 1. Here is the code:

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <WiFiNINA.h>
#include <BlynkSimpleWiFiNINA.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
Adafruit_BME280 bme2;
char auth[] = "wi5JL7Smxw7x6t7kLXkYipbQWU-Ww1PA";
char ssid[] = "WIN_700966";
char pass[] = "tgdxh4p65q";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
bme.begin(0x76);
bme2.begin(0x77);
Blynk.begin(auth, ssid, pass);
}
void loop() {
// put your main code here, to run repeatedly:
Blynk.run();
Blynk.virtualWrite(V2, bme.readTemperature() * 9 / 5 + 32);
Blynk.virtualWrite(V3, bme.readHumidity());
Blynk.virtualWrite(V4, bme.readPressure() / 100.0F);
Blynk.virtualWrite(V0, bme2.readTemperature() * 9 / 5 + 32);
Blynk.virtualWrite(V1, bme2.readHumidity());
}

Please try using the int() function to remove all decimal places.
(Note: The int() function can only output numbers from -32,768 to 32,767. This may cause issues when dealing with numbers that are too large or too small.)
Thanks.

Thanks I am using temp in F so they are pretty high but I figured it out with the (int)trunc function. Do you know how I could use this function but have 1 decimal place?

Well, if you know the number of decimals then you can try -
float y = int(x * z) / z;
and y will be x with 1 decimal place.
( Important - instead of z in this code use 10 to the power of the number of decimals - 1).
Thanks again.

1 Like

it's not uncommon to pass values x10 of x100. the received can do the division

1 Like

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