Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert a Double type to String type with C?

Status
Not open for further replies.

wdshen

Programmer
Sep 12, 2001
1
CN
Except use "sprintf" ,please tell me how to??
 
DO you mean you must use sprintf, or you can't use sprintf?

If you must use sprintf:


#include <string.h>

double dbl_variable = 12.01;
char char_buffer[1024];

sprintf(char_buffer, &quot;%lf&quot;, dbl_variable);

 
You can use the standard library functions for conversions to/from strings/numbers to/from numbers/strings... Ok, here are a set of functions:
from strings to numbers
atof, atoi, _atoi64, atol.
These functions convert strings to integers or floats. Their name comes from:
ansiitofloat,
ansiitoint,
ansiitoiint64bitslong,
ansiitolong

For the reverse conversion u can use:
itoa, fcvt (and its siblings, gcvt and ecvt)

Hope this helps. You can find the documentation for these functions on any compiler help, since they are ANSI (most of them, at least, i think).

This could be a good subject for a FAQ, actually since it is a preety common question in C.



[red]Nosferatu[/red]
We are what we eat...
There's no such thing as free meal...
once stated: methane@personal.ro
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top