user24231: I'm reading from a temperature sensor. I get float values with 2 decimals. I want to round them to nearest 0.1.
This is my 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);
}

This is the output:
21.81 ->21.85
21.79 ->21.75
21.77 ->21.75
21.70 ->21.75


And this is the output I would... Rounded to "0" and not "5".
21.81 ->21.80
21.79 ->21.80
21.77 ->21.80
21.70 ->21.70


user4143: float variables do not have a number of decimal places. They are merely an approximation anyway. Instead it's when you print that you decide how many decimals to display.
Serial.println(temperature, 1); // Print with 1 decimal place

Or to put it into a string:
char buf[8]; // Room for 7 characters plus NULL terminator
float val=23.81;

dtostrf(val, -7, 1, buf); // 7 characters with 1 decimal place and left aligned (negative width)


user40646: I often store the floating point data that I get from a sensor in integer to preserve the decimal precision that I want by multiple it by 10 before casting it into an integer. It is easier and more efficient (compare to string or float) to send over to another system. This is also how many sensors representing and store the floating data (e.g. TMP275) as well.
To achieve what you want, here is what I would do:
#include <math.h>

void setup() {
  Serial.begin(115200);

  float i=21.81;
  float j=21.79;
  float k=21.77;
  float p=21.70;

  // convert to integers
  int l = (int) round(i*10);  // 218
  int m = (int) round(j*10);  // 218
  int n = (int) round(k*10);  // 218
  int o = (int) round(p*10);  // 217

  // Convert back to actual floating point for display
  Serial.println((float)l/10);  //21.80
  Serial.println((float)m/10);  //21.80
  Serial.println((float)n/10);  //21.80
  Serial.println((float)o/10);  //21.70
}


user78331: Using roundf() function:
float rounded_temperature = roundf(temperature * 10) / 10;

24231: <p>I'm reading from a temperature sensor. I get float values with 2 decimals. I want to round them to nearest 0.1.</p>
<p>This is my sketch:</p>
<pre class="lang-c prettyprint-override"><code>void setup() {
  Serial.begin(115200);
}

void loop() {
  float temperature = 21.81;
  float rounded_temperature = round(temperature * 10) / 10.0;
  Serial.print(&quot;21.81 -&gt;&quot;);
  Serial.println(rounded_temperature);
  delay(1000);

  temperature = 21.79;
  rounded_temperature = round(temperature * 10) / 10.0;
  Serial.print(&quot;21.79 -&gt;&quot;);
  Serial.println(rounded_temperature);
  delay(1000);

  temperature = 21.77;
  rounded_temperature = round(temperature * 10) / 10.0;
  Serial.print(&quot;21.77 -&gt;&quot;);
  Serial.println(rounded_temperature);
  delay(1000);

  temperature = 21.70;
  rounded_temperature = round(temperature * 10) / 10.0;
  Serial.print(&quot;21.70 -&gt;&quot;);
  Serial.println(rounded_temperature);
  delay(1000);
}
</code></pre>
<p>This is the output:</p>
<pre><code>21.81 -&gt;21.85
21.79 -&gt;21.75
21.77 -&gt;21.75
21.70 -&gt;21.75

</code></pre>
<p>And this is the output I would... Rounded to &quot;0&quot; and not &quot;5&quot;.</p>
<pre><code>21.81 -&gt;21.80
21.79 -&gt;21.80
21.77 -&gt;21.80
21.70 -&gt;21.70
</code></pre>
 4143: <p><code>float</code> variables do not have a number of decimal places. They are merely an approximation anyway. Instead it's when you <em>print</em> that you decide how many decimals to display.</p>
<pre class="lang-cpp prettyprint-override"><code>Serial.println(temperature, 1); // Print with 1 decimal place
</code></pre>
<p>Or to put it into a string:</p>
<pre class="lang-cpp prettyprint-override"><code>char buf[8]; // Room for 7 characters plus NULL terminator
float val=23.81;

dtostrf(val, -7, 1, buf); // 7 characters with 1 decimal place and left aligned (negative width)
</code></pre>
 40646: <p>I often store the floating point data that I get from a sensor in integer to preserve the decimal precision that I want by multiple it by 10 before casting it into an integer. It is easier and more efficient (compare to string or float) to send over to another system. This is also how many sensors representing and store the floating data (e.g. TMP275) as well.</p>
<p>To achieve what you want, here is what I would do:</p>
<pre><code>#include &lt;math.h&gt;

void setup() {
  Serial.begin(115200);

  float i=21.81;
  float j=21.79;
  float k=21.77;
  float p=21.70;

  // convert to integers
  int l = (int) round(i*10);  // 218
  int m = (int) round(j*10);  // 218
  int n = (int) round(k*10);  // 218
  int o = (int) round(p*10);  // 217

  // Convert back to actual floating point for display
  Serial.println((float)l/10);  //21.80
  Serial.println((float)m/10);  //21.80
  Serial.println((float)n/10);  //21.80
  Serial.println((float)o/10);  //21.70
}
</code></pre>
 78331: <p>Using roundf() function:</p>
<p>float rounded_temperature = roundf(temperature * 10) / 10;</p>
