converting huge #'s in string format to double.

This is a discussion on converting huge #'s in string format to double. within the C++ Programming forums, part of the General Programming Boards category; How do I properly convert huge numbers with decimal points (ex. 14432454554.04524354) in string to double. I tried the function ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    26

    converting huge #'s in string format to double.

    How do I properly convert huge numbers with decimal points
    (ex. 14432454554.04524354) in string to double. I tried the function atof() but it has it's limits. Are there any other way?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    For C way: http://www.rt.com/man/strtod.3.html

    For a C++ way, try adapting this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    sscanf( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    26
    strtod() function looks like it got all the numbers in the double, but
    it change the number with an exponential notation.

    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    int main()
    {
    	char a[] = {"123423423.34343"};
    	char *b;
    
    	double d;
    
    	d = strtod(a, &b);
    
    	cout << d << endl;
    
    	system("PAUSE");
    	return 0;
    }
    
    //the result:
    1.23423e+08
    Press any key to continue . . .
    I'm doing a calculator program and I need to keep everything in order. How the heck am I suppose to output the right answer with the decimal period in the right place.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <cstdio>
    
    using namespace std;
    
    int main()
    {
    
    	char a[] = {"123423423.34343"};
    	
    	double d;
    	
    	sscanf( a, "%lf", &d );
    
    	cout << d << endl;
    	
    	system("PAUSE");
    	return 0;
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    26
    I got the same result from your example XSquared.

    I want this: 123423423.34343 on the output.
    not 1.23423e+08

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    cout.precision( 20 );
    cout << d << endl;
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    26
    Thanks Hammer, didn't know about the precision() function.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM

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