<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Converting float value to string</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/101138/converting-float-value-to-string</link><description>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&amp;#39;t know why the 2nd and 3rd efforts don&amp;#39;t work. 
 
 The output</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Mon, 26 Jun 2023 18:15:04 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/101138/converting-float-value-to-string" /><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/433052?ContentTypeID=1</link><pubDate>Mon, 26 Jun 2023 18:15:04 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:02693c2f-9d1e-4b21-ac4c-caebaa9f41de</guid><dc:creator>garrettb</dc:creator><description>&lt;p&gt;Well, the problem is right now that I can&amp;#39;t use the strtof() function for anything. I get the following error:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;undefined reference to `strtof&amp;#39;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;So, similar to the issue of not being able to use the sprintf() with floats, until I set&amp;nbsp;&lt;span&gt;CONFIG_CBPRINTF_FP_SUPPORT=y in prj.conf, I wonder is there another setting I need to use the strtof() function?&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/432793?ContentTypeID=1</link><pubDate>Sat, 24 Jun 2023 05:14:34 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:5e0d0885-021c-440d-9230-0235ed5ea2c1</guid><dc:creator>Hussam Aldean</dc:creator><description>&lt;p&gt;Take the start of the float number and the end of it, create new string and pass it ti strtof.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/432776?ContentTypeID=1</link><pubDate>Fri, 23 Jun 2023 19:16:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a9c10af0-a415-4fbf-89a2-3e505346a4a1</guid><dc:creator>garrettb</dc:creator><description>&lt;p&gt;Hi Hussam,&lt;/p&gt;
&lt;p&gt;thank you, that worked.&lt;/p&gt;
&lt;p&gt;Now I am trying to reverse the process, and convert a string to a float.&lt;/p&gt;
&lt;p&gt;However if I try use atof() or&amp;nbsp;strtof(), I get an &amp;quot;undefined reference&amp;quot; error.&lt;/p&gt;
&lt;p&gt;Is there another library I need to include, or and extra switch to set in prj.conf?&lt;/p&gt;
&lt;p&gt;Thanks&lt;/p&gt;
&lt;p&gt;Garrett&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/432771?ContentTypeID=1</link><pubDate>Fri, 23 Jun 2023 18:00:59 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:287b4fea-6cc4-4373-9ba6-e18cf12e35af</guid><dc:creator>Hussam Aldean</dc:creator><description>&lt;p&gt;add this to prj.conf&lt;/p&gt;
&lt;p&gt;&lt;span&gt;CONFIG_CBPRINTF_FP_SUPPORT=y&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/432769?ContentTypeID=1</link><pubDate>Fri, 23 Jun 2023 17:57:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:beb82bc0-a8a0-4e56-a7e7-f80815b7420e</guid><dc:creator>garrettb</dc:creator><description>&lt;p&gt;Hi Hussam, thanks for the reply.&lt;/p&gt;
&lt;p&gt;No, that didn&amp;#39;t work for me either.&lt;/p&gt;
&lt;p&gt;I wonder is it to do with includes, or project settings?&lt;/p&gt;
&lt;p&gt;I tried the following code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;    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,&amp;quot;%0.2f&amp;quot;,dbg_float);
    printk(&amp;quot;Result 1: dbg_buf1 = %s\n&amp;quot;, dbg_buf1);
    printk(&amp;quot;Result 2: dbg_float = %0.2f\n&amp;quot;, dbg_float);
    printk(&amp;quot;Result 3: dbg_float = %f\n&amp;quot;, dbg_float);
    sprintf(dbg_buf2,&amp;quot;%0.2f&amp;quot;,dbg_double);
    printk(&amp;quot;Result 4: dbg_buf2 = %s\n&amp;quot;, dbg_buf2);
    printk(&amp;quot;Result 5: dbg_double = %0.2f\n&amp;quot;, dbg_double);
    printk(&amp;quot;Result 6: dbg_double = %f\n&amp;quot;, dbg_double);
    sprintf(dbg_buf3,&amp;quot;%0.2f&amp;quot;,dbg_int);
    printk(&amp;quot;Result 4: dbg_buf3 = %s\n&amp;quot;, dbg_buf3);
    printk(&amp;quot;Result 5: dbg_int = %0.2f\n&amp;quot;, dbg_int);
    printk(&amp;quot;Result 6: dbg_int = %f\n&amp;quot;, dbg_int);
    sprintf(dbg_buf4,&amp;quot;%d&amp;quot;,dbg_int);
    printk(&amp;quot;Result 7: dbg_buf4 = %s\n&amp;quot;, dbg_buf4);
    printk(&amp;quot;Result 8: dbg_int = %d\n&amp;quot;, dbg_int);
    printk(&amp;quot;Result 9: dbg_int = %d\n&amp;quot;, dbg_int);
    &lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;With this result:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;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
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I get the same result if I use LOG_INF() instead of printk().&lt;/p&gt;
&lt;p&gt;Here are the file&amp;#39;s includes:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;#include &amp;lt;bluetooth/services/nus.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;
#include &amp;lt;errno.h&amp;gt;
#include &amp;lt;math.h&amp;gt;
#include &amp;lt;soc.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/bluetooth.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/gatt.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/hci.h&amp;gt;
#include &amp;lt;zephyr/bluetooth/uuid.h&amp;gt;
#include &amp;lt;zephyr/device.h&amp;gt;
#include &amp;lt;zephyr/devicetree.h&amp;gt;
#include &amp;lt;zephyr/logging/log.h&amp;gt;
#include &amp;lt;zephyr/settings/settings.h&amp;gt;
#include &amp;lt;zephyr/sys/printk.h&amp;gt;
#include &amp;lt;zephyr/zephyr.h&amp;gt;
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;And the prj.conf file:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;
# 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

CONFIG_NVS_LOG_LEVEL_DBG=y
CONFIG_REBOOT=y
CONFIG_MPU_ALLOW_FLASH_WRITE=y
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Converting float value to string</title><link>https://devzone.nordicsemi.com/thread/432602?ContentTypeID=1</link><pubDate>Fri, 23 Jun 2023 03:15:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:521ca7a9-2837-4741-9660-8001698111e1</guid><dc:creator>Hussam Aldean</dc:creator><description>&lt;p&gt;How about sprintf(dbg_buf1,&amp;quot;%0.2f&amp;quot;,dbg_float_val); ?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>