# "Round" float to one decimal place for printing to OLED

**URL:** <https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133>\
**Category:** Programming\
**Created:** [September 26, 2021, 5:37am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133 "2021-09-26T05:37:21Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 26, 2021, 5:37am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/1 "2021-09-26T05:37:21Z")

</div>

I would have thought rounding or truncating a number like 71.4323523 to 71.4 would be easy but I've failed to find a short solution.

Is float to string (dtostrf() ) is the only way I can think of. I'm sending the value to an OLED so my understanding sprintf is not applicable.

Is there another solution that is more streamlined?

Thanks

---

<div class="post-metadata">

**Author:** ![johnerrington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnerrington/32/340106_2.png) [@johnerrington](https://forum.arduino.cc/u/johnerrington)\
**Post date:** [September 26, 2021, 5:59am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/2 "2021-09-26T05:59:35Z")

</div>

> [@JohnRob](#):
>
> I'm sending the value to an OLED

Can you post a link to the OLED so we know which one it is; and say which arduino you are using; and post your code?

---

<div class="post-metadata">

**Author:** ![noiasca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/noiasca/32/1126910_2.png) [@noiasca](https://forum.arduino.cc/u/noiasca)\
**Post date:** [September 26, 2021, 6:17am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/3 "2021-09-26T06:17:44Z")

</div>

if your unknown OLED Library for your unknown OLED in your unknown Code is based on print, you can do a simple

```cpp
oled.print(unknownFloat, 1);
```

to get a rounded float printed to the display with one decimal.

---

<div class="post-metadata">

**Author:** ![330R](https://avatars.discourse-cdn.com/v4/letter/3/d78d45/32.png) [@330R](https://forum.arduino.cc/u/330R)\
**Post date:** [September 26, 2021, 6:30am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/4 "2021-09-26T06:30:19Z")

</div>

> [@JohnRob](#):
>
> another solution that is more streamlined?

There are other solutions, but I don't know if you will find them more streamlined or not. Making a buffer with `dtostrf()` is quite streamlined in my view.

However:  
First, you have to decide whether you want rounding or truncating, because they are not the same thing. Then, you can take an approach along these lines:

1. Multiply the float by 10 and cast it to an integer called `foo` (this will truncate it).
2. Compute the integer part as `int integer = foo / 10`
3. Compute the decimal part as `int decimal = foo % 10`
4. Print `integer`, followed by the decimal separator of your choice (point or comma), followed by `decimal`.

If you require rounding, you'll have to take extra steps. Also consider whether your floats will always be positive or if they may be negative too.

Note: You can combine steps 2-4 by `sprintf()`-ing everything into a buffer but at this point... just use `dtostrf()` and it will also round the number for you.

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [September 26, 2021, 7:36am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/5 "2021-09-26T07:36:35Z")

</div>

> [@JohnRob](#):
>
> I'm sending the value to an OLED so my understanding sprintf is not applicable.

I don't understand why you are saying that. I realise there are some limitations using sprintf on some Arduino platforms (not sure what they are), but assuming you can use sprintf then use sprintf. Print to a char array then use the OLED's print function to send the resulting text to the OLED.

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [September 26, 2021, 7:50am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/6 "2021-09-26T07:50:36Z")

</div>

Multiply it by 10, round it to an int then divide that by 10 to get your float?

-jim lee

---

<div class="post-metadata">

**Author:** ![330R](https://avatars.discourse-cdn.com/v4/letter/3/d78d45/32.png) [@330R](https://forum.arduino.cc/u/330R)\
**Post date:** [September 26, 2021, 9:57am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/7 "2021-09-26T09:57:18Z")

</div>

> [@PerryBebbington](#):
>
> I realise there are some limitations using sprintf on some Arduino platforms (not sure what they are)

I found at my own expenses what they are. What I thought was a bug in my code that was really bugging me (pun intended), was a silly limitation of the IDE's version of `sprintf()`: it won't take a float for an argument. Hence, the necessity of an intermediate buffer generated by `dtostrf()` that is then passed to `sprintf()` as a string.

The fact that the OP is sending the string to an OLED is irrelevant, of course: the problem is upstream.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 26, 2021, 10:08am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/8 "2021-09-26T10:08:40Z")

</div>

i'm told support for floats in sprintf() is removed on Arduino.

[dtostrf()](https://www.programmingelectronics.com/dtostrf/) can be used

```cpp
  f = 1.3

```

```cpp

char s [80];
char t [10];

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

    float f = 1.27;

    dtostrf (f, 5, 1, t);
    sprintf (s, " f = %s", t);
    Serial.println (s);
}

void loop () {
}

```

---

<div class="post-metadata">

**Author:** ![PerryBebbington](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/perrybebbington/32/238766_2.png) [@PerryBebbington](https://forum.arduino.cc/u/PerryBebbington)\
**Post date:** [September 26, 2021, 10:16am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/9 "2021-09-26T10:16:11Z")

</div>

> [@gcjr](#):
>
> i'm told support for floats in sprintf() is removed on Arduino.

I'm aware there are some limits to its use, but I don't know what they are. I have used sprintf with Arduino successfully so, whatever the limits are I am yet to come up against them. However, @JohnRob commented that he didn't think he could use sprintf with and OLED, not that he couldn't use it with an Arduino. My point is that providing the limits on using it with Arduino don't apply then it is perfectly possible to use it with an OLED in the way I suggested.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [September 26, 2021, 10:19am UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/10 "2021-09-26T10:19:12Z")

</div>

i have no problem passing a c-string to the OLED devices i've used. doesn't matter what is used to construct/format the string.

---

<div class="post-metadata">

**Author:** ![JohnRob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnrob/32/253560_2.png) [@JohnRob](https://forum.arduino.cc/u/JohnRob)\
**Post date:** [September 26, 2021, 4:14pm UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/11 "2021-09-26T16:14:28Z")

</div>

Thanks for the responses.

The code @noiasca's recommended works fine on my 16 x 32 generic OLED. And I am embarrassed I didn't read further on the print function reference page. I only remembered the 2nd parameter as specifying the base ☹

For future reference:  
The OLED driver is from Bill Greiman.

```cpp
// ARDUINO - AHT20 - OLED
// Target processor: Pro Mini 3v3
//
#include <Wire.h>

//OLED driver from Bill Greiman.
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"

#define OLED_ADDRESS 0x3C
// Define proper RST_PIN if required.
#define RST_PIN -1

#include "AHT20.h"

AHT20 AHT;
SSD1306AsciiWire oled;

void setup()
{
    Serial.begin(9600);
    delay(500);
    Serial.println("AHT20 Sensor");

    AHT.begin();

    oled.begin(&Adafruit128x32, OLED_ADDRESS);
    oled.setFont(CalLite24);
    oled.displayRemap(true); // rotate text 180°
    oled.clear();
}

void loop() (
    float humi, temp;
    int ret = AHT.getSensor(&humi, &temp);
    
    if(ret) { // GET DATA OK
// temp = temp *9/5+32;
       temp = 74.12345;
       oled.SSD1306Ascii::setCursor(1*8,0);
       oled.print(temp,1);
    }
    else { // GET DATA FAIL
        Serial.println("GET DATA FROM AHT20 FAIL");
    }
    
    delay(10000);
}

// --- eof ---

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 25, 2022, 4:15pm UTC](https://forum.arduino.cc/t/round-float-to-one-decimal-place-for-printing-to-oled/909133/12 "2022-03-25T16:15:06Z")

</div>

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