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
Exit0815
Senior

Hi,

what about?

 

Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, tempUnit);

 

float tempValue = 32.45f; // Example temperature value as a float

const char* tempUnit; // Pointer to the temperature unit (Celsius or Fahrenheit)

tempUnit = "degC"; or tempUnit = "degF";

 

AScha.3
Chief III

Hi,

and you have enabled print-float ?

-> use float with printf...

AScha3_0-1727104143954.png

 

If you feel a post has answered your question, please click "Accept as Solution".

Thanks for the response,

I tried it but its throwing error as ''no matching function for call to snprintfFloat" (argument types are: (uint16_t [11], uint16_t const, char const [8], float, char const *))

 

FYI, I'm using TouchGfx 4.21.3 for designing LCD screens for my project with ST Microcontroller and updating part of code in Container with two textAreas.

 

My code:

float tempValue = 32.45f;

tempUnitsTitleType tempUnitsDescTable[] = {
{1, "degC"},
{2, "degF"}, 

};

 

const char* GetTempUnitsString(int8_t enumValue)

{ // Iterate through the table to find the matching enum value

     for (size_t i = 0; i < sizeof(tempUnitsDescTable) / sizeof(tempUnitsDescTable[0]); i++)

    {

           if (tempUnitsDescTable[i].enumValue == enumValue)

           {

              return tempUnitsDescTable[i].unitString;

           }

   } // Return NULL if no matching enum value is found return NULL;

}

 

Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, GetTempUnitsString(1);

 

Thanks for the response.

I'm not sure where to set this option? can you please provide more details.

As all other settings...

in IDE, select your project as "active now" , by having the main.c open or click in left window on the project name;

then menu->project->properties

select ... (as in pic) C/C++ build -> settings -> MCUsettings -> X use float with printf

 

(then float lib is included in build for this project )

If you feel a post has answered your question, please click "Accept as Solution".

Please insert code samples with the "insert/Edit Code sample" button:

unsigned_char_array_0-1727177467372.png

This is how I print temperatures:

	textArea1.invalidateContent();
	int32_t tempInt = int32_t(roundf(m_temperature*10));
	Unicode::snprintf(textArea1Buffer, TEXTAREA1_SIZE, "%3d.%d%c%c", tempInt/10, abs(tempInt)%10, 0x00B0, m_unitFahrenheit ? 'F' : 'C'); // 0x00B0 is degree symbol
	//textArea1.resizeToCurrentText();  // in case of right align use resizeToCurrentTextWithAlignment()
	textArea1.resizeToCurrentTextWithAlignment();
	textArea1.invalidateContent();

 

snprintfFloat only supports floats: https://support.touchgfx.com/docs/api/classes/classtouchgfx_1_1_unicode#function-snprintffloat
You can probably print just the number to a temporary string and then print again using snprintf and %s. But I just converted it to an integer instead.
Good luck!

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.
GaetanGodart
ST Employee

Hello @BhavyaSri ,

 

I am able to print a float by doing :

    float myVal = 37.05;
    Unicode::snprintfFloat(textArea1Buffer, TEXTAREA1_SIZE, "%.2f", myVal);
    textArea1.invalidate();

Please find attached the whole project.

 

I hope this helps! :smiling_face_with_smiling_eyes:
If this comment or another one answers your question, I invite you to select it as "best answer".

 

Regards,

Gaetan Godart
Software engineer at ST (TouchGFX)

Thanks for the reply, I wanted to print not only a float but also a string value also along with the float value like "32.45 degC"  or "90.41 degF"(based on other function output to print degC or deg F). 

 

My Current Code:

float tempValue = 32.45f;

tempUnitsTitleType tempUnitsDescTable[] = {
{1, "degC"},
{2, "degF"}, 
};

const char* GetTempUnitsString(int8_t enumValue)
{ // Iterate through the table to find the matching enum value

     for (size_t i = 0; i < sizeof(tempUnitsDescTable) /sizeof(tempUnitsDescTable[0]); i++)
    {
      if (tempUnitsDescTable[i].enumValue == enumValue)
      {
         return tempUnitsDescTable[i].unitString;
      }

   } // Return NULL if no matching enum value is found return NULL;

}

 

Unicode::snprintfFloat(textArea2Buffer, TEXTAREA2_SIZE, "%.2f %s", tempValue, GetTempUnitsString(1);

 

This is not printing units but it throws error 'no matching function for call to snprintfFloat"

Read my comment.

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.