Converting float value to string

Hi

I am using nRF Connect SDK, wioth nRF5340_DK.
I am trying to convert a float value to a string, with two decimal places.
Have a look at the code below, and then see the outputs.
I don't know why the 2nd and 3rd efforts don't work.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
float dbg_float_val;
char dbg_buf1[8];
int dbg_len1;
char dbg_buf2[8];
int dbg_len2;
char dbg_buf3[8];
int dbg_len3;
dbg_float_val = 1.234F;
int wholePart = (int)dbg_float_val;
int decimalPart = (int)((dbg_float_val - wholePart) * 100); // Multiply by 100 to get two decimal places
dbg_len1 = snprintf(dbg_buf1, sizeof(dbg_buf1), "%d.%02d", wholePart, decimalPart);
printf("Result 1: dbg_buf1 = %s, dbg_len1: %d\n", dbg_buf1, dbg_len1);
dbg_len2 = sprintf(dbg_buf2,"%.2f", dbg_float_val);
printf("Result 2: dbg_buf2 = %s, dbg_len2: %d\n", dbg_buf2, dbg_len2);
dbg_len3 = snprintf(dbg_buf3, sizeof(dbg_buf3), "%.2f", dbg_float_val);
printf("Result 3: dbg_buf3 = %s, dbg_len3: %d\n", dbg_buf3, dbg_len3);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The output I get:

Fullscreen
1
2
3
Result 1: dbg_buf1 = 1.23, dbg_len1: 4
Result 2: dbg_buf2 = %.2f, dbg_len2: 4
Result 3: dbg_buf3 = %.2f, dbg_len3: 4
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • How about sprintf(dbg_buf1,"%0.2f",dbg_float_val); ?

  • Hi Hussam, thanks for the reply.

    No, that didn't work for me either.

    I wonder is it to do with includes, or project settings?

    I tried the following code:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    char dbg_buf1[16];
    char dbg_buf2[16];
    char dbg_buf3[16];
    char dbg_buf4[16];
    char dbg_buf5[16];
    float dbg_float = 12.345;
    double dbg_double = 54.321;
    int dbg_int = 1357;
    sprintf(dbg_buf1,"%0.2f",dbg_float);
    printk("Result 1: dbg_buf1 = %s\n", dbg_buf1);
    printk("Result 2: dbg_float = %0.2f\n", dbg_float);
    printk("Result 3: dbg_float = %f\n", dbg_float);
    sprintf(dbg_buf2,"%0.2f",dbg_double);
    printk("Result 4: dbg_buf2 = %s\n", dbg_buf2);
    printk("Result 5: dbg_double = %0.2f\n", dbg_double);
    printk("Result 6: dbg_double = %f\n", dbg_double);
    sprintf(dbg_buf3,"%0.2f",dbg_int);
    printk("Result 4: dbg_buf3 = %s\n", dbg_buf3);
    printk("Result 5: dbg_int = %0.2f\n", dbg_int);
    printk("Result 6: dbg_int = %f\n", dbg_int);
    sprintf(dbg_buf4,"%d",dbg_int);
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    With this result:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    Result 1: dbg_buf1 = %0.2f
    Result 2: dbg_float = %0.2f
    Result 3: dbg_float = %f
    Result 4: dbg_buf2 = %0.2f
    Result 5: dbg_double = %0.2f
    Result 6: dbg_double = %f
    Result 4: dbg_buf3 = %0.2f
    Result 5: dbg_int = %0.2f
    Result 6: dbg_int = %f
    Result 7: dbg_buf4 = 1357
    Result 8: dbg_int = 1357
    Result 9: dbg_int = 1357
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    I get the same result if I use LOG_INF() instead of printk().

    Here are the file's includes:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    #include <bluetooth/services/nus.h>
    #include <dk_buttons_and_leds.h>
    #include <errno.h>
    #include <math.h>
    #include <soc.h>
    #include <stdio.h>
    #include <zephyr/bluetooth/bluetooth.h>
    #include <zephyr/bluetooth/gatt.h>
    #include <zephyr/bluetooth/hci.h>
    #include <zephyr/bluetooth/uuid.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/logging/log.h>
    #include <zephyr/settings/settings.h>
    #include <zephyr/sys/printk.h>
    #include <zephyr/zephyr.h>
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    And the prj.conf file:

    Fullscreen
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    # Enable bonding
    CONFIG_BT_SETTINGS=y
    CONFIG_FLASH=y
    CONFIG_FLASH_PAGE_LAYOUT=y
    CONFIG_FLASH_MAP=y
    CONFIG_NVS=y
    CONFIG_SETTINGS=y
    # Enable DK LED and Buttons library
    CONFIG_DK_LIBRARY=y
    # This example requires more workqueue stack
    CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048
    # Config logger
    CONFIG_LOG=y
    CONFIG_USE_SEGGER_RTT=y
    CONFIG_LOG_BACKEND_RTT=y
    CONFIG_LOG_BACKEND_UART=n
    CONFIG_ASSERT=y
    XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

  • add this to prj.conf

    CONFIG_CBPRINTF_FP_SUPPORT=y

  • Hi Hussam,

    thank you, that worked.

    Now I am trying to reverse the process, and convert a string to a float.

    However if I try use atof() or strtof(), I get an "undefined reference" error.

    Is there another library I need to include, or and extra switch to set in prj.conf?

    Thanks

    Garrett

  • Take the start of the float number and the end of it, create new string and pass it ti strtof.

1 2