Printing floating point numbers using u8g2lib.h

Hey guys,

is there a direct way to send floating point (eg. 1.23) numbers to a display using u8g2lib.h?

Up to now, I split the variable into integer and decimal part by using modulo operation and so on but I've got the feeling, that there must be a better solution. Unfortunately google & co was not helpful until now...

Any recomondation? Thanks a lot!

U8g2 supports the standard Arduino Print command. It is also able to print float values.

Oliver

most examples use "serial.print" but I want to send to display.
Is there some kind of "display.print" command?
Would appreciate if someone has a template that offers to set the number decimals

e.g.
float A = 1.234;
int d = 2;
"display.print(A,d)"

--> output 1.23

Is there some kind of "display.print" command?

This is what i tried to say. U8g2 exactly includes the same print command and will put the output on the display.

Oliver

due to I'm still at the beginning getting into C++ and therefore asking for a template, the last answer linking to "serial.print" isn't a help at all. anyway, thanks for your response!

Could someone please post an example how to solve the task as described in the first post?

linking to "serial.print" isn't a help at all.

Sometimes i am really impressed by the replys. And I really wonder whether it is worth to answer questions here? Ok, forget all my answers. The only answer to your initial question is this link:

Maybe some other reader can answer the initial question. I probably should take a rest from this forum :wink:

Oliver

const double myFloat = 123.4567;
myU8G2libObject.print(myFloat,2);

Unable to test but that should display "123.46" where you had the cursor positioned in the current font and color. Is it clear now? If not, try explaining what part you don't understand.

avr_fred:

const double myFloat = 123.4567;

myU8G2libObject.print(myFloat,2);

thank's a lot avr_fred and olikraus! must be annoying to you experts to answer 'stupid' questions. I'm still beginner and don't get it.

defining 'myFloat' is clear.
copy-pasting 'myU8G2libObject.print(myFloat,2);' leads to error message. "not declared in this cope"

would expect something like "u8g2.print(50,60,myFloat,2);" since u8g2.drawStr(0,24,"Hello World!"); works fine (0,24 = position)

thank's a lot avr_fred and olikraus! must be annoying to you experts to answer 'stupid' questions.

It's not annoying, as long as you would read the manual: "u8g2.print(50,60,myFloat,2);" will not work.
You must use "u8g2.setCursor(50,60); u8g2.print(myFloat,2);" as described in the manual.

Oliver

Probably initially it would have been okay to just say:

Use this:

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

And then see where it went.
Saying "It uses Arduino print" left it open to the "serial.print" confusion.

But, I have a friend who says I over think everything. So...

as a beginner I was stuck too by the u8g2.print command, because the compiler refused with the annoying message that "...yxz was not declared".
... BUT the Serial.print command showed the desired output(float) in the serial monitor???

Reading this here solved my confusion, and maybe this will help others having similar problems:
https://forum.arduino.cc/index.php?topic=394938.0