943,672 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 18972
  • C RSS
Jul 1st, 2007
0

Float/Double to String conversion

Expand Post »
Is it possible to convert Float or double in a string???

Any idea??
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gemni7 is offline Offline
5 posts
since Jul 2007
Jul 1st, 2007
0

Re: Float/Double to String conversion

Try sprintf()
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Jul 1st, 2007
0

Re: Float/Double to String conversion

> 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.  */
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 2nd, 2007
0

Re: Float/Double to String conversion

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gemni7 is offline Offline
5 posts
since Jul 2007
Jul 2nd, 2007
0

Re: Float/Double to String conversion

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.
Aia
Reputation Points: 2224
Solved Threads: 218
Nearly a Posting Maven
Aia is offline Offline
2,304 posts
since Dec 2006
Jul 3rd, 2007
0

Re: Float/Double to String conversion

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.
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,717 posts
since May 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help needed
Next Thread in C Forum Timeline: polynomial ADT





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC