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 105 views
5 answers 377 views
377 views asked Jun 20, 2021 by avibootz
2 answers 215 views
215 views asked May 16, 2021 by avibootz
2 answers 146 views
2 answers 150 views
150 views asked May 17, 2021 by avibootz
...