2024-09-23 03:32 AM
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
2024-09-24 07:22 AM
I cannot round off the value to nearest integer, it has to be precisely displayed as is.
My understanding of your code:
2024-09-24 07:41 AM - edited 2024-09-25 05:23 AM
@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 .
2024-09-24 07:51 AM
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);
2024-09-30 07:37 AM
Hello @BhavyaSri ,
Were you able to move forward on your issue?
Regards,