How to convert a number from stringstream to string with a setprecision in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>
#include <sstream>

int main() {
    float f = 3.141592653589;
    std::stringstream ss;
    
    ss.precision(3);
    ss << f;
    std::string s = ss.str();

    std::cout << s;

    return 0;
}



/*
run:

3.14

*/

 



answered May 9, 2021 by avibootz

Related questions

2 answers 351 views
351 views asked May 17, 2021 by avibootz
2 answers 259 views
259 views asked May 17, 2021 by avibootz
1 answer 250 views
1 answer 231 views
1 answer 182 views
1 answer 188 views
188 views asked Jan 13, 2023 by avibootz
...