Round with one decimal point

I have problem. SHT temperature is 24.37 and rouding temp = 24.37 and it should have been 24.3.

Why does it still show two decimal places?

I have code:
float temp = (sht.getTemperature() * 10) / 10;
float temp = (raw * 10) / 10;

What shows 2 decimal places? Your float will store the actual value to several decimal places.

If you are referring to the serial monitor printing, then I believe that printing a float defaults to 2 DP.

If you just want to print to 1 decimal place:
Serial.print(temp, 1);

1 Like

I don't want serial.print, I only need one number after the decimal point. I write data to the ePaper display.

Do you write it, or do you print it?

(I only ask because I can't see your code)

Then it would be:
lcd.print(temp, 1);

If that is not what you want, tell us what LCD you have and what LCD library that you are using.

It is the syntax of the print statement. So even if you were writing to a WiFi client it would be:
client.print(temp, 1);

1 Like

SHT3x thermometer measures 24.53. I'll do the rounding and I should get 24.5. But arduino converts it to 24.50 - it adds zero at the end automatically. Can it be deleted?

float RAWteplotaIN = sht.getTemperature() * 10; // 24.53*10
int RRWteplotaIN = (int)RAWteplotaIN; // 245
teplotaIN = (float)RRWteplotaIN / 10; // 24.50 -> add zero, why?

Probably time to show us your code rather than a few lines ...

teplotaIN is a floating point number. The only difference between 24.5 and 24.50 is how you display it or print it. You could even display 24.5000 and it doesn't change teplotaIN.

Just a guess here as I have not info on your libraries. However you might have to change the float to a string or char array to get the formatting you want.

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