How to display a float/int?

Description

How do I print a float or an int with a label (I understand this is the default object for displaying text)? Is this functionality not build in to the library?

On my Teensy display library I just call display.print(float1,1);
On LVGL I’ve seen a suggestions to use sprintf but I tried it and it didn’t seem to work.

I’ve been through documentation a number of times but can’t understand how to do this.

What LVGL version are you using?

Latest

You can use printf on a label with lv_label_set_text_fmt.

@embeddedt thank you!
Is there a way to fix the position of the print to the decimal point? or will I have to do that manually by changing the position of the object based on the number of characters being printed? Or maybe just align the whole object to the right?

I think our printf implementation supports padding the output string with spaces; that may help? I’m not sure how you would line up the decimal points though.

don`t work with float

float  bla = 255.55;
lv_obj_t * label1 = lv_label_create(lv_scr_act(), NULL);
lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
lv_label_set_text_fmt(percentage_power,"%f",bla );
lv_obj_align(label1, NULL, LV_ALIGN_CENTER, 0, 0);

image

In lv_conf.h try setting #define LV_SPRINTF_CUSTOM to 1

1 Like

Floating-point is disabled by default for code size reasons; you can re-enable it by changing LV_SPRINTF_DISABLE_FLOAT in your lv_conf.h file.

2 Likes

thank you)