Getting zero result after using sprintf for converting double to char array

Hi, 

I am currently trying to convert a double value to char array. But looks like there is a time that my conversion works properly, and also there is a time that I got wrong result, like the result is 0.00000 or something like that.

So below is my proj.conf

#
# Copyright (c) 2021 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# General config
CONFIG_FPU=y
CONFIG_NEWLIB_LIBC=y
CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
CONFIG_NRF_MODEM_LIB=y

# Heap and stacks
CONFIG_MAIN_STACK_SIZE=2048
# Extended memory heap size needed for encoding REST messages to JSON
CONFIG_HEAP_MEM_POOL_SIZE=8192
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=1536
# Increase AT monitor heap because %NCELLMEAS notifications can be large
CONFIG_AT_MONITOR_HEAP_SIZE=512

# Location library
CONFIG_LOCATION=y

# Logging
CONFIG_LOG=y
CONFIG_LOG_MODE_IMMEDIATE=y
CONFIG_RTT_CONSOLE=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y

# MQTT
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=n
CONFIG_MQTT_CLEAN_SESSION=y
CONFIG_PDN=y
CONFIG_PDN_LEGACY_PCO=y
CONFIG_NET_SOCKETS_POSIX_NAMES=y

# Network
CONFIG_NETWORKING=y
CONFIG_NET_NATIVE=n
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_OFFLOAD=y

# LTE link control
CONFIG_LTE_LINK_CONTROL=y
CONFIG_LTE_NETWORK_MODE_NBIOT_GPS=y
CONFIG_LTE_AUTO_INIT_AND_CONNECT=n
CONFIG_LTE_EDRX_REQ=y
# Request PSM active time of 8 seconds.
CONFIG_LTE_PSM_REQ_RAT="00000100"

# AT Host library - Used to send AT commands directy from an UART terminal and to allow
#		    integration with nRF Connect for Desktop LTE Link monitor application.
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_AT_HOST_LIBRARY=y

# nRF Cloud (for A-GPS and cell location)
CONFIG_NRF_CLOUD_REST=y
CONFIG_NRF_CLOUD_AGPS=y
CONFIG_NRF_CLOUD_CLIENT_ID_SRC_COMPILE_TIME=y
CONFIG_NRF_CLOUD_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
CONFIG_MODEM_INFO=y # required by CONFIG_NRF_CLOUD_AGPS
CONFIG_MODEM_INFO_ADD_NETWORK=y # required by CONFIG_NRF_CLOUD_AGPS

# Library that maintains the current date time UTC for A-GPS and P-GPS purposes
CONFIG_DATE_TIME=y

# Modem JWT
CONFIG_MODEM_JWT=y

# HWID
CONFIG_HW_ID_LIBRARY=y
CONFIG_HW_ID_LIBRARY_SOURCE_DEVICE_ID=y
# Multicell location service selection
CONFIG_MULTICELL_LOCATION_SERVICE_NRF_CLOUD=y

So below is part of my code which is trying to convert double to a char array.

			printk("Publishing location using ");
			printk("cellular service.\n");

			printk("Publishing longitude %f ", event_data->location.longitude);
			sprintf(temp_lon, " %f ", event_data->location.longitude);
			printk("value : %s ", temp_lon);
			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_lon, sizeof(temp_lon), TOPIC_LOC_CELLULER_LON);
			if (err) {printk("ERR data_publish (longitude) : %d", err);} else {printk(" -- success.\n");}

			printk("Publishing latitude %f ", event_data->location.latitude);
			sprintf(temp_lat, " %f ", event_data->location.latitude);
			printk("value : %s ", temp_lat);
			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_lat, sizeof(temp_lat), TOPIC_LOC_CELLULER_LAT);
			if (err) {printk("ERR data_publish (longitude) : %d", err);} else {printk(" -- success.\n");}

			printk("Publishing both information ");
			sprintf(temp_all, " %f,%f ", event_data->location.latitude, event_data->location.longitude);
			printk("value : %s ", temp_all);
			err = data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, temp_all, sizeof(temp_all), TOPIC_LOC_CELLULER);
			if (err) {printk("ERR data_publish (both) : %d", err);} else {printk(" -- success.\n");}		

So from my code, as you can see, I print double value first then print again converted value (as char array). But when I check on rtt, the result is 0.000000. Below is the result that I receive using rtt.

Publishing Location using cellular service.
Publishing longitude 46.69XXXX value :  0.000000   -- success.
Publishing latitude  24.79XXXX value :  0.000000   -- success.
Publishing both information value :  0.000000,46.69XXXX   -- success.
Requesting location with the default configuration...

I edit a little location by using X as mark to do censorship on location.

Anyway, that is the issue that I got when using sprintf. Anybody have an idea about this? thank you so much for your help, really appreciate.

Parents Reply Children
No Data
Related