How to convert a double to string in c++

Started by
4 comments, last by LordG 22 years ago
Does anybody know how? Is there a function to do this?
Advertisement
_gcvt
Converts a floating-point value to a string, which it stores in a buffer.

char *_gcvt( double value, int digits, char *buffer );

Routine Required Header Compatibility
_gcvt Win 95, Win NT
You could use sprintf(target string, format string, double value);

But I'm sure there are other ways, too.

Yeah, like that one

[edited by - Waverider on March 21, 2002 11:27:28 PM]
It's not what you're taught, it's what you learn.
How do I get rid of the padding zeros when converting a double to a string. For example, this code prints out 134.560000.


    int main(){	char str[20];	double x = 134.56;		sprintf(str, "%f", x);			cout << str << endl;		return 0;}    


[edited by - LordG on March 21, 2002 11:53:03 PM]
Use %g instead of %f.

std::stringstream

  #include <sstream>#include <string>int main(){  float f = 1.23456;  std::stringstream ss;  ss << f;  std::cout << ss.str() << std::endl;}  


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement