How to convert float to string with 2 decimal places in C++

1 Answer

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

int main() {
    float f = 782305.8731;
    
    std::stringstream ss;
    
    ss << std::fixed << std::setprecision(2) << f;
    
    std::string s = ss.str();
    
    std::cout << s;
}




/*
run:

782305.88

*/

 



answered Feb 12, 2023 by avibootz

Related questions

1 answer 222 views
1 answer 319 views
2 answers 376 views
2 answers 369 views
2 answers 414 views
...