944,157 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 15209
  • C++ RSS
Oct 23rd, 2007
0

Cast double to const char*

Expand Post »
I would like to know once and for all how to convert a double to a const char*

I've tried it a few times, but have never succeeded, I know it can be done, but I haven't figured out how to yet. I've tried some different things. Sometimes it won't compile, and sometimes the program crashes, and other times it ends up as an empty string.
Similar Threads
Reputation Points: 61
Solved Threads: 5
Junior Poster
prushik is offline Offline
101 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Cast double to const char*

U can try sprintf().

Amit
Last edited by amt_muk; Oct 23rd, 2007 at 4:13 am.
Reputation Points: 14
Solved Threads: 3
Light Poster
amt_muk is offline Offline
48 posts
since May 2005
Oct 23rd, 2007
0

Re: Cast double to const char*

depends on how you want to use it. If you want to pass a pointer to a double as a parameter to some function that takes char* then typecasting might work, depending on what that function is going to do with it. Normally, however, you will have to convert it storing the results in either a character array or a std::string object.

Two ways come to mind: sprintf() will convert and store in a character array, while std::stringstream will convert and store in std::string object.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Oct 23rd, 2007
0

Re: Cast double to const char*

>I would like to know once and for all how to convert a double to a const char*
Casting doesn't work. You're not converting the double to a string with a cast, you're telling the compiler to use the binary representation of the double as characters in a string. For a value like 12.35, that's not going to give you a string like "12.35". You need to take the printed value (like if you write the double using cout) and store it in a string. That's really really painful to do manually with floating-point, so you should use sprintf or stringstream like Ancient Dragon said.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 23rd, 2007
0

Re: Cast double to const char*

Here's an interesting discussion on the topic:
http://www.codeproject.com/string/st...onversions.asp
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Nov 12th, 2008
0

Re: Cast double to const char*

http://www.cplusplus.com/reference/c...o/sprintf.html
____________________
Le Parkour LifeStyle
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Seta00 is offline Offline
1 posts
since Nov 2008

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: can anyone help me solving this program ??plz
Next Thread in C++ Forum Timeline: Need insight in assignment





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


Follow us on Twitter


© 2011 DaniWeb® LLC