cancel
Showing results for 
Search instead for 
Did you mean: 

How to print float value and units in single text area

BhavyaSri
Associate III

Hi,

I'm looking to print value as "32.45 degC"  or "90.41 degF"(based on other function output to print degC or deg F) in textArea2 but not able to print using Unicode::snprintfFloat. Can you please help me in printing float value and its units in textarea2.

 

If the value is in decimal, I'm able to print using below logic and print the output as 32 degC or 90 degF:

Unicode::snprintf(textArea2Buffer,TEXTAREA2_SIZE,"%d %s",tempValue, tempUnit);

 

Thanks ,

Bhavya

13 REPLIES 13

I cannot round off the value to nearest integer, it has to be precisely displayed as is.

My understanding of your code:

  • %3d: This formats an integer (tempInt / 10) as a decimal number with a minimum width of 3 characters (right-aligned). If the number is smaller, it will be padded with spaces on the left.
  • .: A literal dot (.) is inserted to separate the integer part from the fractional part of the temperature.
  • %d: This formats another integer (abs(tempInt) % 10) as a decimal number, representing the fractional part of the temperature.
  • %c (for 0x00B0): This inserts the degree symbol (°), which is represented by the Unicode value 0x00B0.
  • %c (for m_unitFahrenheit ? 'F' : 'C')

 

 


@BhavyaSri wrote:

I cannot round off the value to nearest integer, it has to be precisely displayed as is


The number you want to print is IEE754 floating point single precision which cannot accurately represent decimal fractions. So there is always rounding going on. If you leave out the explicit round it will simply be rounded down.
32.459 degC would then be displayed as 32.45 degC. If that is fine just leave out the rounding. Just note that %.2f also rounds, so 32.459 is printed as 32.46.

You want two decimal places. So you would need to do zeropadding for the second number. Example:

 

#include <stdio.h>
#include <stdlib.h>

int main()
{
    for(int i=-999; i<1000; ++i)
    {                
        int32_t tempInt = i;//int32_t(m_temperature*100);
        float f = (float) i / 100;
    	printf("%4.2f = %3d.%02d\n", f, tempInt/100, abs(tempInt)%100);     	
    }
}

 

tested in cpp.sh

In TouchGFX it would be something like (untested) :

 

float tempValue = 32.45f;

const Unicode::UnicodeChar* GetTempUnitsString(int8_t enumValue);
 
int32_t tempInt = tempValue*100;
Unicode::snprintf(textArea2Buffer, TEXTAREA2_SIZE, "%3d.%02d %s", tempInt/100, abs(tempInt)%100, GetTempUnitsString(1);

 

Alternatively you can print in two steps (untested):

 

Unicode::UnicodeChar floatBuffer[6+1];
Unicode::snprintfFloat(floatBuffer, sizeof(floatBuffer), "%3.2f", tempValue);
Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%s %s",floatBuffer, GetTempUnitsString(1)); 

 

%3 or %3 is not needed in either example if you don't want padding with spaces (see my topic on alignment: https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/digit-alignment-not-optimal-because-quot-figure-space-quot-not/m-p/716723#M39275)

Note that %s needs a unicodestring so you need to modify GetTempUnitsString to return unicode string. You can use https://support.touchgfx.com/docs/api/classes/classtouchgfx_1_1_unicode#fromutf8 for that. Or read https://community.st.com/t5/stm32-mcus-touchgfx-and-gui/unicode-snprintf-string-problem/m-p/280777 .

Kudo posts if you have the same problem and kudo replies if the solution works.
Click "Accept as Solution" if a reply solved your problem. If no solution was posted please answer with your own.

Unicode | TouchGFX Documentation 4.21

%s or any other isnt valid in this float class func. When you require manage it in C strings or other many existed ways. Simple one variant :

if (tempunitdegC)
   snprintfFloat(buffer, 20, "%6.2f degC", 3.14f);
else
   snprintfFloat(buffer, 20, "%6.2f degF", 3.14f);
GaetanGodart
ST Employee

Hello @BhavyaSri ,

 

Were you able to move forward on your issue?

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)