How to convert float to string in C++

2 Answers

0 votes
#include <iostream>   
 
int main() {
  std::string pi = "pi = " + std::to_string(3.14159265359);
  
  std::cout << pi;
}
 
 
 
/*
run:
 
pi = 3.141593
 
*/

 



answered May 16, 2021 by avibootz
edited May 11, 2024 by avibootz
0 votes
#include <iostream>
#include <sstream>

int main() {
    double d = 23.8713;
  
    std::ostringstream os;
      
    os << d;
      
    std::string s = os.str();
     
    std::cout << s;
}
 
 
 
/*
run:
 
23.8713
 
*/

 



answered May 11, 2024 by avibootz

Related questions

1 answer 105 views
5 answers 377 views
377 views asked Jun 20, 2021 by avibootz
2 answers 150 views
2 answers 145 views
2 answers 150 views
150 views asked May 17, 2021 by avibootz
...