Thread: atof() precision issue

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    2

    atof() precision issue

    string line2 = "-89.75131467880756";
    cout<<line2<<" <> ";
    xcord = atof(line2.c_str());
    cout<<xcord<<endl;


    output -
    -89.75131467880756 <> -89.7513


    Question is ... how do i get it to desplay more precision.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    This should do it:

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main (void)
    {
    	double xcord;
    	string line2 = "-89.75131467880756";
    	cout<<line2<<" <> ";
    	xcord = atof(line2.c_str());
    	cout<<setprecision(16) << xcord<<endl;
    	return 0;
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    2
    Such a simple fix..... Thank you sooooo much =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Setting Precision Confusion
    By dnguyen1022 in forum C++ Programming
    Replies: 11
    Last Post: 01-14-2009, 10:38 AM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  4. Precision based floating-point
    By Mario F. in forum C++ Programming
    Replies: 4
    Last Post: 07-17-2006, 10:35 AM
  5. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM