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 355 views
355 views asked May 17, 2021 by avibootz
2 answers 262 views
262 views asked May 17, 2021 by avibootz
1 answer 255 views
1 answer 235 views
1 answer 186 views
1 answer 192 views
192 views asked Jan 13, 2023 by avibootz
...