Olimex Support Forum

Microcontrollers => PIC => Topic started by: kngoc on June 21, 2016, 12:36:05 AM

Title: Function convert real number to string
Post by: kngoc on June 21, 2016, 12:36:05 AM
In pic-maxi-web, temperature values are convert from integer to string by funtion: itoa(temperature, TempString);
But I want to show temperature values are real numbers (ex.: 32.6).
I set the temperature variable is a float and use funtion RTOS, but there is no function RTOS in Helpers.c
How to solve this problem?
Looking forward to your help. Thank you.
Title: Re: Function convert real number to string
Post by: JohnS on June 21, 2016, 11:02:14 AM
Various C functions exist so use the one you want.

John
Title: Re: Function convert real number to string
Post by: kyrk.5 on June 21, 2016, 02:47:37 PM
Hi,

I think this might be convenient:
http://www.cplusplus.com/reference/cmath/modf/

For PIC usually there is a float/double version of the function also available. After the conversion the int part can be casted to integer, the fraction part can be *10 and then converted to integer. That way you get 88.8
88.88 Is more tricky. You need to *10 and then convert it to integer and print with %2.2u to see the leading zeros. That way you get 88.08 also.

By the way, is sprintf not able to handle float numbers directy?
Title: Re: Function convert real number to string
Post by: kngoc on June 21, 2016, 04:14:31 PM
I want to display on the Web page (WebPages2 - index.htm - Temperature:?) by command: <p> Temperature: <span id = "temp0" style = "font-weight: normal">? </ Span> </ p>, not the print command.
Thank you for your feedback.




































I want to display on the Web page (WebPages2 - index.htm - Temperature:?) By command: <p> Temperature: <span id = "temp0" style = "font-weight: normal">? </ Span> </ p>, not the print command.
Thank you for your feedback.









I want to display on the Web page (WebPages2 - index.htm Temperature:?) By command: <p> Temperature: <span id = "temp0" style = "font-weight: normal">? </ Span> </ p>, not the print command.
Thank you for your feedback.









I want to display on the Web page (WebPages2 - index.htm Temperature:?) By command: <p> Temperature: <span id = "temp0" style = "font-weight: normal">? </ Span> </ p>, not the print command.
Thank you for your feedback.