How to convert float number to string in C++

2 Answers

0 votes
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

int main()
{
	float f = 3.14;

	string s = std::to_string(f);

	cout << s << endl;

	return 0;
}

/*
run:

3.140000

*/

 



answered May 9, 2018 by avibootz
0 votes
#include <iostream>
#include <string>
#include <sstream>

using std::cout;
using std::endl;
using std::string;

int main()
{
	float f = 3.14;

	std::ostringstream oss;

	oss << f;

	string s = oss.str();

	cout << s << endl;

	return 0;
}

/*
run:

3.14

*/

 



answered May 9, 2018 by avibootz

Related questions

1 answer 137 views
5 answers 482 views
482 views asked Jun 20, 2021 by avibootz
2 answers 253 views
253 views asked May 16, 2021 by avibootz
2 answers 189 views
2 answers 189 views
189 views asked May 17, 2021 by avibootz
...