If you really want to print a float, which is far from clear, you should find an example of that included in whatever library you are using, and it should be a lot simpler than what you are doing. I can't imagine there is a library that cannot do this, and you are probably just not looking hard enough.
Sorry guys, I am a bit new to this. I probably generated some confusion.
The float is needed in the original temp calculation. I had hoped to just print that to the OLED.
That said, as I dont need a decimal point and doing some more digging after your comments, I believe I can use CAST to get an integer from the float then use ITOA again?
Yes, you can indeed use a CAST operator, for instance like: (int) float
But that will just truncate the float removing the decimal part.
So depending on the required accuracy this might be acceptable.
That's the great part about providing complete information. With a little digging, you'll see that Adafruit_SSD1351 inherits from Adafruit_SPITFT:
class Adafruit_SSD1351 : public Adafruit_SPITFT {
Adafruit_SPITFT inherits from Adafruit_GFX:
class Adafruit_SPITFT : public Adafruit_GFX {
And Adafruit_GFX inherits from Print
class Adafruit_GFX : public Print {
So, it already knows how to print a float (as I conjectured in Reply #4). So, all you have to do is:
tft.println(temperature);
or:
tft.println(temperature, n);
where 'n' is the number of digits you want to the right of the decimal point (could be 0 if you like). The result will be rounded accordingly. No conversions or casts required.
kpg:
Sorry 1 last question so that I totally understand....
So how does
class Adafruit_GFX : public Print {
tell me it is able to print floats directly?
Since Adafruit_GFX inherits from Print, it knows how to do what Print does. From that, there are a couple of ways to determine it can print a float. These you will only pick up from experience:
You know that the standard "Serial" object is of a class that also (indirectly) inherits from Print. So, since Serial's class and Adafruit_GFX both inherit from the same base class AND Serial knows how to print a float, then chances are that objects of the Adafruit_GFX class do also.
You could look inside Print.h and find the prototype:
size_t print(double, int = 2);
On an 8-bit AVR, float and double are the same thing. Even on 32-bit processors, a float will be implicitly up-cast to a double when passed to this function.