Welcome, Guest

Author Topic: Function convert real number to string  (Read 1943 times)

kngoc

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Function convert real number to string
« 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.

JohnS

  • Hero Member
  • *****
  • Posts: 1725
  • Karma: +33/-48
Re: Function convert real number to string
« Reply #1 on: June 21, 2016, 11:02:14 AM »
Various C functions exist so use the one you want.

John

kyrk.5

  • Jr. Member
  • **
  • Posts: 62
  • Karma: +3/-0
Re: Function convert real number to string
« Reply #2 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?

kngoc

  • Newbie
  • *
  • Posts: 2
  • Karma: +0/-0
Re: Function convert real number to string
« Reply #3 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.