user68265: I'm trying to set my temperature measurements to two decimal places. Currently what I get are six decimal places.
Here is my code:
#include "math.h"

...

(in loop)
temp1 = sensors.getTempCByIndex(0);
     temp1 = roundf(temp1*100)/100;

Once I push temp1 to Firebase realtime database there are sic decimals, such as 26.059999. I'm using dsb20 sensor for temperature measurements.
What could be wrong?
Thanks for your help in advance!

user4143: float variables don't have a number of decimal places: that's how they get their name: "floating point" - the decimal point floats around as needed.
It's only when you output the value that it's rendered with a fixed (or limited) number of decimal places.  Using Serial.print() the second digit is the number of decimal points:
Serial.println(temp1, 6); // 26.059999
Serial.println(temp1, 2); // 26.06


user42015: In addition to Majenkos answer:
A float cannot take any decimal number. It has a limited change and a limited resolution in that range you could say. So if often happens, that the flat (rounded) value is not a valid float. This is the case for 26.05. There is no valid float value, that is exactly 26.05. So the nearest valid float value is chosen. The following code would always print 26.059999:
float i = 26.05;
Serial.println(i);

since the variable i contains the float number 26.059999 (nearest valid float value to 26.06). That is the reason, why mostly the rounding of floats is only done directly with the outputting/printing. In that stage the number is not longer bound to the limitations of the float type (since it is internally not again saved in that type).

68265: <p>I'm trying to set my temperature measurements to two decimal places. Currently what I get are six decimal places.
Here is my code:</p>
<pre><code>#include &quot;math.h&quot;

...

(in loop)
temp1 = sensors.getTempCByIndex(0);
     temp1 = roundf(temp1*100)/100;
</code></pre>
<p>Once I push temp1 to Firebase realtime database there are sic decimals, such as 26.059999. I'm using dsb20 sensor for temperature measurements.
What could be wrong?</p>
<p>Thanks for your help in advance!</p>
 4143: <p><code>float</code> variables don't have a number of decimal places: that's how they get their name: &quot;floating point&quot; - the decimal point floats around as needed.</p>
<p>It's only when you <em>output</em> the value that it's rendered with a fixed (or limited) number of decimal places.  Using <code>Serial.print()</code> the second digit is the number of decimal points:</p>
<pre class="lang-cpp prettyprint-override"><code>Serial.println(temp1, 6); // 26.059999
Serial.println(temp1, 2); // 26.06
</code></pre>
 42015: <p>In addition to Majenkos answer:</p>
<p>A <code>float</code> cannot take any decimal number. It has a limited change and a limited resolution in that range you could say. So if often happens, that the flat (rounded) value is not a valid <code>float</code>. This is the case for <code>26.05</code>. There is no valid <code>float</code> value, that is exactly <code>26.05</code>. So the nearest valid <code>float</code> value is chosen. The following code would always print <code>26.059999</code>:</p>
<pre><code>float i = 26.05;
Serial.println(i);
</code></pre>
<p>since the variable <code>i</code> contains the <code>float</code> number <code>26.059999</code> (nearest valid <code>float</code> value to <code>26.06</code>). That is the reason, why mostly the rounding of <code>float</code>s is only done directly with the outputting/printing. In that stage the number is not longer bound to the limitations of the <code>float</code> type (since it is internally not again saved in that type).</p>
