Ciao a tutti,
leggo da un sensore di temperature e ottengo un float a 2 decimali (p.e. 21.77). Voglio arrotondare al decimale più vicino.
Questo è il mio sketch
void setup() {
Serial.begin(115200);
}
void loop() {
float temperature = 21.81;
float rounded_temperature = round(temperature * 10) / 10.0;
Serial.print("21.81 ->");
Serial.println(rounded_temperature);
delay(1000);
temperature = 21.79;
rounded_temperature = round(temperature * 10) / 10.0;
Serial.print("21.79 ->");
Serial.println(rounded_temperature);
delay(1000);
temperature = 21.77;
rounded_temperature = round(temperature * 10) / 10.0;
Serial.print("21.77 ->");
Serial.println(rounded_temperature);
delay(1000);
temperature = 21.70;
rounded_temperature = round(temperature * 10) / 10.0;
Serial.print("21.70 ->");
Serial.println(rounded_temperature);
delay(1000);
}
E questo è l'output
21.81 ->21.85
21.79 ->21.75
21.77 ->21.75
21.70 ->21.75
Mentre l'output che vorrei è...
21.81 ->21.80
21.79 ->21.80
21.77 ->21.80
21.70 ->21.70
Grazie a tutti!