Skip to main content
Angle
Associate III
December 20, 2022
Solved

how to display the value in float using snprintf?

  • December 20, 2022
  • 3 replies
  • 10834 views

Hi, how can i use the snprintf for float value in touchgfx? I want to display the data from the sensor in touchgfx lcd but in float

This topic has been closed for replies.
Best answer by ktrofimo

it also depends on what C library is used. Nano version doesn't have float support. Besides of this TouchGFX requires Unicode strings.

So, I use

Unicode::snprintfFloat(buffer, bufSize, "%f", var );

3 replies

gbm
Lead III
December 20, 2022

And why don't you check snprintf() in C standard library reference? There are no mysteries - just read the documents. Print formats are common fo all *printf functions. To use floating point formats you must enable float printf library support as suggested by the compiler - look at the compiler messages when using the %f format for the first time.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
ktrofimo
ktrofimoBest answer
Senior III
December 20, 2022

it also depends on what C library is used. Nano version doesn't have float support. Besides of this TouchGFX requires Unicode strings.

So, I use

Unicode::snprintfFloat(buffer, bufSize, "%f", var );

Angle
AngleAuthor
Associate III
December 20, 2022

i have tried it before but its display % instead of float number from the sensor

KiptonM
Senior III
December 21, 2022

They are telling you to do this:

Project->Properties->C/C++ Build->Settings->Tool Settings(Tab)->MCU Settings

Then check the box that says Use float with printf from newlib-nano (-u _printf_float)

Yoann KLEIN
ST Employee
January 2, 2023

Hello @Angle​ ,

I just tried it with snprintfFloat and it works. Also, it is specified in the documentation that snprintf only works with %c (element type: char), %s (element type: null-terminated UnicodeChar list), %u, %i, %d, %o, %x formats.

Please let us know if that solved your issue.

/Yoann

Yoann KLEINST Software Developer | TouchGFX
Angle
AngleAuthor
Associate III
January 12, 2023

hi, so I need to convert floating-point to char first right? let say I have value -1.23, so I need to convert one by one right? can I get any example or where to refer on how to convert it ?

Yoann KLEIN
ST Employee
January 12, 2023

Hello,

If you use snprintfFloat you don't have to convert anything, it will work with your float value.

/Yoann

Yoann KLEINST Software Developer | TouchGFX