convert char* to double

Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

convert char* to double

 
0
  #1
Jun 12th, 2007
hey, how would i go about converting a char* character array to a double.
I would like to create a function that takes a char array as a parameter such as "1234.1" and then return that as a double value 1234.1.

Is there a function that would allow me to do this or would i have to go about programming it myself?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,540
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: convert char* to double

 
1
  #2
Jun 12th, 2007
>hey, how would i go about converting a char* character array to a double.
Provided the array is actually a string with a terminating '\0' character at the end, you can use strtod. Or, since this is C++, stringstreams make the conversion intuitive if you're used to cin and cout:
  1. #include <sstream>
  2.  
  3. double to_double ( const char *p )
  4. {
  5. std::stringstream ss ( p );
  6. double result = 0;
  7.  
  8. ss>> result;
  9.  
  10. return result;
  11. }
Last edited by Narue; Jun 12th, 2007 at 12:32 pm.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC