How to make arithmetic operations with float or double

Important: unclear posts may not receive useful answers.

Before posting

  • Get familiar with Markdown to format and structure your post
  • Be sure to update lvgl from the latest version from the master branch.
  • Be sure you have checked the FAQ and read the relevant part of the documentation.
  • If applicable use the Simulator to eliminate hardware related issues.

Delete this section if you read and applied the mentioned points.

Description

What MCU/Processor/Board and compiler are you using?

Adruino Giga R1 with giga display shield on arduino ide 2.3.6

What LVGL version are you using?

9.2.2

What do you want to achieve?

I want to set (with embedded keyboard) a float or double value on a text label, then multiplie them, and print the result on another label.

What have you tried so far?

All i want above, works perfect with integer type, but never with float or double. I extract the string with “lv_label_get_text” then convert it with “atoi” fonction (and made an operation).
I try fonction “atof” “strtod” “strtof” and it always print a big value (10 or 12 digits before dot when i expect 2).
I change the defaut code compiler by -std=gnuC99 : Works a little better but not as expected.

Code to reproduce

I don’t expect you to debug my code problem. I’m pretty sur that’s simple and i miss something basic.
So my question is : Show me an exemple of code (2 or 3 lines) of what you do for extract a value (float) from a label.
I search an exemple since one month but never find it.

Thanks

static lv_obj_t* label1;
static lv_obj_t* label2;
static lv_obj_t* label_result;

void btn_event_cb(lv_event_t* e)
{
lv_event_code_t code = lv_event_get_code(e);

if (code == LV_EVENT_CLICKED) {
    const char* str1 = lv_label_get_text(label1);
    const char* str2 = lv_label_get_text(label2);
    float num1 = atof(str1);
    float num2 = atof(str2);

    float result = num1 * num2;

    char buf[32];
    snprintf(buf, sizeof(buf), "%.2f", result);
    lv_label_set_text(label_result, buf);
}

}

void create_ui(void)
{
lv_obj_t* cont = lv_obj_create(lv_screen_active());
lv_obj_set_size(cont, 200, 200);
lv_obj_center(cont);

// label1
label1 = lv_label_create(cont);
lv_label_set_text(label1, "4.14"); 
lv_obj_align(label1, LV_ALIGN_TOP_LEFT, 10, 20);

// label2
label2 = lv_label_create(cont);
lv_label_set_text(label2, "2.2");  
lv_obj_align(label2, LV_ALIGN_TOP_RIGHT, -10, 20);

// btn
lv_obj_t* btn = lv_btn_create(cont);
lv_obj_align(btn, LV_ALIGN_CENTER, 0, 0);
lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_CLICKED, NULL);

// btn_label
lv_obj_t* btn_label = lv_label_create(btn);
lv_label_set_text(btn_label, "Multiply");
lv_obj_center(btn_label);

// result_label
label_result = lv_label_create(cont);
lv_label_set_text(label_result, "0.00");  
lv_obj_align(label_result, LV_ALIGN_BOTTOM_MID, 0, -20);

}

you may need to enable LV_USE_FLOAT in lv_conf.h

Thanks for your replie.
I try this and it didn’t work. For example, when i multiplie 5.8 x 12.1 the printed result is 12 or 14 digits and always terminated by .00
things i try :
-I obviously enable LV_USE_FLOAT in lv_conf9.h
-Change float type by double type (and of course changing the function).
-Modifiy

#define LV_USE_STDLIB_SPRINTF    LV_STDLIB_BUILTIN 
to
#define LV_USE_STDLIB_SPRINTF     LV_STDLIB_CLIB
or
#define LV_USE_STDLIB_SPRINTF     LV_STDLIB_CUSTOM

-Add in lv_conf.h

#define LV_SPRINTF_CUSTOM 1
#if LV_SPRINTF_CUSTOM
    #define LV_SPRINTF_INCLUDE <stdio.h>
    #define lv_snprintf  snprintf
    #define lv_vsnprintf vsnprintf
#else   /*LV_SPRINTF_CUSTOM*/
    #define LV_SPRINTF_USE_FLOAT 1
#endif  /*LV_SPRINTF_CUSTOM*/

#define LV_USE_USER_DATA 1

-Change <stdio.h> by <stdlib.h> : no compilation
-Add “#include <stdlib.h>” in the .ino file : Same problem.
-Change “atof()” by “strtof()” : arduino stop working.

I think i miss a parameter in some configuration file but where ? Is possibly a conflict with parameter files between arduino_H7_video library and lvgl library ?

So.
After many hours to make some tests, I can say :
atof() function DIDN’T work on my board.
I use instead the sscanf function.

here the code work as i want :

include "ui.h"

void calcul(lv_event_t* e) {
  const char* str1 = lv_label_get_text(ui_LabelVal1);
  const char* str2 = lv_label_get_text(ui_LabelVal2);

  float num1, num2;
  sscanf(str1, "%f", &num1);
  sscanf(str1, "%f", &num2);

  float result = num1 * num2;

  lv_label_set_text_fmt(ui_LabelResult, "%.3f", result);
}

Problem solved :slight_smile:

PS :

#include <stdio.h>

at top of your file

1 Like