Using strtod() C function to convert strings to a double-precision value

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows 2003 Server Standard Edition

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info:

To do: Converting a string to a double value using strtod() C function

To show: How to use strtod() C function to convert a string to double value

 

 

// Using strtod()/wcstod() - string to double conversion

#include <stdio.h>

 

int main(void)

{

double p;

char *thestring = "41.2% sample string";

char *thestringPtr;

 

p = strtod(thestring, &thestringPtr);

 

printf("Using strtod() - converting string to double...\n");

printf("-------------------------------------------\n\n");

printf("The string \"%s\" is converted to the\n", thestring);

printf("double value %.2f and the string \"%s\" \n", p, thestringPtr);

return 0;

}

 

Output example:

 

Using strtod() - converting string to double...

-------------------------------------------

The string "41.2% sample string" is converted to the

double value 3415728.00 and the string "% sample string"

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index