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:
Multiply the float by 10 and cast it to an integer called foo (this will truncate it).
Compute the integer part as int integer = foo / 10
Compute the decimal part as int decimal = foo % 10
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.
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.
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.
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.
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.
// 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 ---