Printing float values on Grove OLED U8g2lib

Happy New Year to All,

I am reading values such as GPS and environmental data which will be ‘float’ format. I have a small 0.96" OLED (SSD1315 monochrome) using the U8g2 library.

I can display ‘dummy’ values such as “123.456789” if they are text but the library doesn’t support float values. If I try to use a float it fails to compile (as expected really):

"Compilation error: no matching function for call to ‘U8G2_SSD1306_128X64_NONAME_F_HW_I2C::drawStr(int, int, float&)’

Is there an easy way to do this? ie convert a float to a text string? Possibly one before the decimal point and one after? Or some other solution?

#include <U8g2lib.h>

float testval=123.456789

u8g2.clearBuffer();                                           // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr);            // choose a suitable font
u8g2.drawStr(0,20,"Sample");                         //text works fine
u8g2.drawStr(0,30,"Date 23/12/2024");          //text works fine
u8g2.drawStr(0,40,"GPS Lo 0.00 La 0.00");   //DUMMY 'values' for GPS in text format works fine
u8g2.drawStr(0,50,"Light " light" Lux");           //text works fine
u8g2.drawStr(0,60,"T \ H 15C 30%");             //DUMMY 'values' for Temp and Humidity in text format works fine
u8g2.drawStr(0,10,testval);                             //FAILS trying to send a real value but it is in 'float' format

u8g2.sendBuffer();

SOLVED:

I did some more searching and I can do this as follows:

u8g2.setCursor(x,y);
u8g2.print(var);

I just have to print the text and then overlay the values using ‘print’ once the cursor is in the correct location!

1 Like