Skip to main content
gamedev.net
🔒 Locked

double to string ??

Started by malpass 11 replies 600+ views
Original Post
malpass
malpass
how do I convert a double to a string, itoa is for int but I dont know for doubles or floats etc..
Br1
Br1
how do I convert a double to a string, itoa is for int but I dont know for doubles or floats etc..

If you are using c++, take a look at boost''s lexical_cast lbrary
at boost.org.

Hope it helps
Bruno Martínez
Fruny
Fruny
iota() isn''t ANSI, only the char* → number conversion functions are.

In C, use sprintf().
In C++, use stringstream.


  
#include <sstream>
#include <string>

using namespace std;

int main()
{
stringstream ss;
double d = 415.234;
string str;

ss << d;
ss >> str;
}



  
#include <stdio.h>

int main()
{
char buf[256];
double d = 415.234;

snprintf( buf, 256, "%lf", d );
};



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
malpass
malpass
nope, i havent got a clue wat that site is on about, it just looks a mess. I just want to know the line to convert a double to string, anyone. MSDN is never a help, I NEVER find what i''v after on there
Lektrix
Lektrix
I don''t know of a function that converts a double to a string, but I''m sure someone must of made one. I''ve heard of a function for converting floats to strings; you can just convert the double to float, depending on how much accuracy you need. Maybe you should just search for dtoa() or ftoa() on a search engine. Good luck.

:::: Lektrix ::::
[ Google || ACCU || BarrysWorld || E-Mail Me ]
Arild Fines
Arild Fines
quote:
Original post by malpass
MSDN is never a help, I NEVER find what i''v after on there

But is that MSDN''s fault, or yours?



"The fact that a believer is happier than a skeptic is no more to the point than the fact that a drunken man is happier than a sober one." -- George Bernhard Shaw
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
malpass
malpass
well i tryed that stringstream and I got 6 errors, stringstream undeclared (the header is included), some errors about it not then finding the ss variable, << illegal left operand has type double, >> illegal righthand operand has type double etc...
malpass
malpass
fixed that, i missed using namespace, anyway, now its just displaying in int''s not float or double?
eldee
eldee
double mydouble = 9.1234567
char* mystring;

sprintf(mystring, "%d", mydouble);

cout << mystring << endl;

---------------------------------------------------
"9.1234567"

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

malpass
malpass
done it, it was showing ints because up above in my code I used atoi to convert the GetDlgItemText to doubles, which was actually converting them to int
civguy
civguy
quote:
Original post by eldee
double mydouble = 9.1234567
char* mystring;

sprintf(mystring, "%d", mydouble);

cout << mystring << endl;
You''d better allocate some memory.
dalleboy
dalleboy
Even better is to use the snprintf function instead of sprintf if it exists, and it usually does. It''s not standard C++ yet (it is standard in C99 though), but you''ll gain buffer length checking.



Update GameDev.net system time campaign: ''date ddmmHHMMYYYY''
IFooBar
IFooBar
all these are for converting double to string
1)ecvt() - Convert double to string of specified length
2)_fcvt() - Convert double to string with specified number of digits following decimal point
3)_gcvt() - Convert double number to string; store string in buffer

and if you want to makea float into a string then cast it to a double and use the above functions. You wont loose any precision that way.

I dont think the above functions are standard though. Search MSDN for parameter info.



:::Al:::
[Triple Buffer V2.0] - Resource leak imminent. Memory status: Fragile
[size=2]aliak.net

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.