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 100 views
1 answer 150 views
2 answers 169 views
2 answers 155 views
2 answers 176 views
...