2

I am using stringstream to parse float from string.

std::stringstream strs(cp.val);
strs.precision(5);
strs<<std::setprecision(5);
float x; strs>>x;

however the set precision functions do not seem to work.. is there any way to force precision in parsing not printing ?

5
  • I assume you want to read at most 5 digits after the decimal point? Is there a specific reason for that? Commented Oct 16, 2014 at 9:38
  • 3
    Why you dont' just round the value after parsing? Commented Oct 16, 2014 at 9:40
  • @AdrianMaire Because that would be lying to the user? If I input 3.1415926536, I don't expect to get 3.14159. If there's some reason why input should be limited to five digits after the decimal (and fixed format? or is 1.5e-100 acceptable?), then this test must be done on the text input, and the user must get an error message if he enters something invalid. Commented Oct 16, 2014 at 9:47
  • @James Kanze: Not at all, setting a precision to a double may have a lot of purposes, so "lying to the user" is only if the user may expect to get a full value. In most cases, the user do not expect to get 23.999999999999 for a simple operation, so a round may solve this issue. Or maybe there is not user at all: the program only parse a file input and make a conversion (e.g. from string file to binary file?) Commented Oct 16, 2014 at 10:00
  • @AdrianMaire That's why I put the question mark after that sentence. A lot depends on context; from his question (which maybe doesn't describe his real problem), I gather that he wants to limit the input to five decimals, in which case, accepting more and then rounding them away isn't a valid solution. More generally, however, there are certainly a lot of cases where it is. Commented Oct 16, 2014 at 11:35

2 Answers 2

3

The precision (along with things like fixed and scientific) only affect output; input will parse anything that looks like a numeric value, extracting all of the characters which could possibly be part of any numeric value. (This includes hex characters, and the 'X' which might occur for integral input.) If you want to limit the format in any way, you'll have to read it as a string, validate the format, and then use std::istringstream to convert it.

Sign up to request clarification or add additional context in comments.

Comments

1

stringstram does not allow precision on parsing, but you may set the double precision after parsing.

E.g.

#include <iostream>
#include <sstream>
#include <iomanip>
#include <cmath>

using namespace std; //do not use that in real project.

int main()
{
    // Initial declarations
    stringstream ss("13.321646");
    double x = 0.0;

    // Parse the double
    ss >> x;
    // Set the double precision to 2decimals
    double roundedX = round(x*100)/100;

    // output solution
    cout << x << endl;
    cout << setprecision(5) << x << endl;
    cout << roundedX << endl;

    return 0;
}

Obvious note: That allow to reduce precision, not to improve it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.