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 222 views
222 views asked May 17, 2021 by avibootz
2 answers 154 views
154 views asked May 17, 2021 by avibootz
1 answer 163 views
1 answer 146 views
1 answer 96 views
1 answer 91 views
91 views asked Jan 13, 2023 by avibootz
...