DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   convert char* to double (http://www.daniweb.com/forums/thread80754.html)

bops Jun 12th, 2007 12:08 pm
convert char* to double
 
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.

Narue Jun 12th, 2007 12:32 pm
Re: convert char* to double
 
>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:
#include <sstream>

double to_double ( const char *p )
{
  std::stringstream ss ( p );
  double result = 0;

  ss>> result;

  return result;
}


All times are GMT -4. The time now is 5:07 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC