| | |
Float/Double to String conversion
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
> Any idea??
C Syntax (Toggle Plain Text)
#include <stdio.h> int main( void ) { float d_n = 123.45; char s_cp[13] = { '\0' }; char s_cnp[4] = { '\0' }; /* * with sprintf you need to make sure there's enough space * declared in the array */ sprintf( s_cp, "%.2f", d_n ); printf( "%s\n", s_cp ); /* * snprinft allows to control how much is read into array. * it might have portable issues if you are not using C99 */ snprintf( s_cnp, sizeof s_cnp - 1 , "%f", d_n ); printf( "%s\n", s_cnp ); getchar(); return 0; } /* output : * 123.45 * 123 */
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
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
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
![]() |
Similar Threads
Other Threads in the C Forum
- Previous Thread: Help needed
- Next Thread: What is Static functions
Views: 7092 | Replies: 5
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi






