How to concatenate two floats into a string in C++

1 Answer

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

int main() {
    float f1, f2;

    f1 = 31.748832;
    f2 = -893.98351;

    std::stringstream stream;
    stream << std::fixed << std::setprecision(4) << f1 << " " << f2;
    std::string s = stream.str();
    
    std::cout << s;
    
    return 0;
}



/*
run:

31.7488 -893.9835

*/

 



answered May 3, 2021 by avibootz

Related questions

1 answer 164 views
1 answer 241 views
1 answer 206 views
1 answer 165 views
1 answer 153 views
4 answers 342 views
342 views asked May 10, 2021 by avibootz
1 answer 190 views
190 views asked Jan 5, 2021 by avibootz
...