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 231 views
1 answer 329 views
1 answer 293 views
1 answer 232 views
1 answer 222 views
4 answers 497 views
497 views asked May 10, 2021 by avibootz
1 answer 262 views
262 views asked Jan 5, 2021 by avibootz
...