Float/Double to String conversion

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2007
Posts: 5
Reputation: gemni7 is an unknown quantity at this point 
Solved Threads: 0
gemni7 gemni7 is offline Offline
Newbie Poster

Float/Double to String conversion

 
0
  #1
Jul 1st, 2007
Is it possible to convert Float or double in a string???

Any idea??
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Float/Double to String conversion

 
0
  #2
Jul 1st, 2007
Try sprintf()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Float/Double to String conversion

 
0
  #3
Jul 1st, 2007
> Any idea??

  1. #include <stdio.h>
  2.  
  3. int main( void )
  4. {
  5. float d_n = 123.45;
  6. char s_cp[13] = { '\0' };
  7. char s_cnp[4] = { '\0' };
  8.  
  9. /*
  10.   * with sprintf you need to make sure there's enough space
  11.   * declared in the array
  12.   */
  13. sprintf( s_cp, "%.2f", d_n );
  14. printf( "%s\n", s_cp );
  15.  
  16. /*
  17.   * snprinft allows to control how much is read into array.
  18.   * it might have portable issues if you are not using C99
  19.   */
  20. snprintf( s_cnp, sizeof s_cnp - 1 , "%f", d_n );
  21. printf( "%s\n", s_cnp );
  22.  
  23. getchar();
  24. return 0;
  25. }
  26.  
  27. /* output :
  28.  * 123.45
  29.  * 123
  30.  */
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 5
Reputation: gemni7 is an unknown quantity at this point 
Solved Threads: 0
gemni7 gemni7 is offline Offline
Newbie Poster

Re: Float/Double to String conversion

 
0
  #4
Jul 2nd, 2007
Thanks Aia,

I am not using C99 compiler. I am writing code for micro controller. I have very limited memory to accomplish task. snprintf is not working with my compiler. Is snprintf memory constraint?

Is there any way to get the job done utilizing minimum memory??

I thank you for your help.

rgds
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,048
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 179
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Float/Double to String conversion

 
0
  #5
Jul 2nd, 2007
Mr. Gemni7 I do not know the answer to your questions. All that I know is that for compilers previous to the ISO C99 standards, snprintf() is not an obtion. In this case you will have to use the more portable function sprintf. Making sure you allow for enough space in the array to hold the float convertion.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,133
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Float/Double to String conversion

 
0
  #6
Jul 3rd, 2007
Separate the number into 2 values at the decimal.
Convert the numbers into single integers using / and * and %
Convert each single digit to character (add 48)
Load the character values into your output array
Last edited by WaltP; Jul 3rd, 2007 at 12:28 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 7092 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC