Advertisement Jump to content
Sign in to follow this  
jenny_wui

Some clarification about string to double conversion

This topic is 1887 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

     I have the following piece of code:

string ss = findNodeValue( str, "Horizon");
cout << "ss is: " << ss << endl;
double dd = atof( ss.c_str());
cout << "dd is: " << dd << endl;

When the value of 'ss' is printed, I find it prints 1.0, but when the value of 'dd' is printed, it prints 1 whereas it is supposed to print 1.0. Could any one help me figure out what the problem is.

 

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

Why is it "supposed" to print 1.0? The value "1" can be represented exactly in a double, and you didn't specify any particular format, so it just prints that number in the most natural way possible, that is, "1" (actually, I think it is implementation dependent). If ss contained "1.0000000000000000000000000000000000000000000000000000000000000000000000000000000000" would you argue that dd should also be printed exactly the same?

 

If you want to obtain a certain precision/number of digits, you can tell cout that by passing stream modifiers, e.g. std::setprecision, etc..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!