Need help: converting string to double

This is a discussion on Need help: converting string to double within the C Programming forums, part of the General Programming Boards category; Hi, all. I do search the forum before raise my problem. I need to convert a string to a double, ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    South Pacific Island
    Posts
    9

    Need help: converting string to double

    Hi, all. I do search the forum before raise my problem. I need to convert a string to a double, but the atof() or strtod() is not work. Why? What is wrong with my code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int 
    main (int argc, char *argv[])
    {
    	char *pr0 = "0.05";
    	char *ptr;
    	double pr2, pr3;
    	pr2 = atof(pr0);
    	pr3 = strtod(pr0, &ptr);
    	printf("The number from atof()   is: %d\n", pr2);
    	printf("The number from strtod() is: %d\n", pr3);
    	return 0;     
    }
    Here is the output after compiled and run:

    The number from atof() is: -1717986918
    The number from strtod() is: -1717986918
    Anyone can help?

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Use %f in your prinf for a double, not %d that's for signed integers.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    South Pacific Island
    Posts
    9
    Thank you so much. It work now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 01:04 PM
  2. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 03:56 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. converting string to double
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-17-2002, 05:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21