Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8709177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:18:16+00:00 2026-06-13T04:18:16+00:00

double val = 0.1; std::stringstream ss; ss << val; std::string strVal= ss.str(); In the

  • 0
double val = 0.1;
std::stringstream ss;
ss << val;
std::string strVal= ss.str();

In the Visual Studio debugger, val has the value 0.10000000000000001 (because 0.1 can’t be represented).
When val is converted using stringstream, strVal is equal to "0.1". However, when using boost::lexical_cast, the resulting strVal is "0.10000000000000001".

Another example is the following:

double val = 12.12305000012;

Under visual studio val appears as 12.123050000119999, and using stringstream and default precision (6) it becomes 12.1231. I don’t really understand why it is not 12.12305(…).

Is there a default precision, or does stringstream have a particular algorithm to convert a double value which can’t be exactly represented?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:18:17+00:00Added an answer on June 13, 2026 at 4:18 am

    You can change the floating-point precision of a stringstream as follows:

    double num = 2.25149;
    std::stringstream ss(stringstream::in | stringstream::out);
    ss << std::setprecision(5) << num << endl;
    ss << std::setprecision(4) << num << endl;
    

    Output:

    2.2515
    2.251
    

    Note how the numbers are also rounded when appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing strange results when doing a string.Format( C ); E.g. double val =
I have a typedef boost::variant<int, float, double, long, bool, std::string, boost::posix_time::ptime> variant which I
I have some financial data gathered at a List[(Int, Double)], like this: val snp
Double has range more than a 64-bit integer, but its precision is less dues
I found this code online as a template for doing a string to float/int/double
char in[100], *temp[10],var[10][10]; int i, n = 0, double val[10]; var[0][]=ANS; I want to
I have a double as the value in hashmap with the key as a
As you know, float point has a precision problem, that is a value 1
I've been trying to call the overloaded table::scan_index(std::string, ...) member function without success. For
class Message{ double *val }; Message** val_msgs; val_msgs = new Message*[120]; for(int i=0; i<120;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.