Set Precision command when converting float value into string

Ok, I am trying to make a string of values inputted by users.
Since the values might include decimal values, so I am forced to use float variables.
however, whenever I use, to_string function for conversion, it converts the floating value into string but adds way too many 0s.
I don't want that many 0s. In fact, I don't even want a single 0 if a value is simply an integer value.. and even if it is a floating value then I don't want a decimal value beyond 2 numbers.
But how can I do this?
Anybody here who can help me with that?

Here's my code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <iomanip>
using namespace std; 

int main()
{
	string f; 
	float in, po;
	cout << "Enter Variable's Constant: "; 
	cin >> in; 
	cout << "Enter Variable's Power: "; 
	cin >> po;
	while (1)
	{
		if (in > 0)
			f.append("+");
		setprecision(2);
		f.append(to_string(in));
		if (po != 0)
		{
			f.append("x"); 
			if (po != 1)
			{
				f.append("^");
				f.append(to_string(po));
			}
		}
		system("cls");
		cout << "Your Current Function: " << f << endl << endl;
		cout << "Enter Variable's Constant: ";
		cin >> in;
		if (in == 0)
			break;
		cout << "Enter Variable's Power: ";
		cin >> po;
	}
	cout << endl; 
	system("pause");
	return 0; 
}


Here's the output after first input,

1
2
3
Your Current Function: +3.000000x^2.000000

Enter Variable's Constant: 

As you can see, there are too many 0s.. Where, in fact, I don't even want a single 0 there. It should be like this instead,

1
2
3
Your Current Function: +3x^2

Enter Variable's Constant: 


Anybody who can help with this? Thank you!



to_string() does not have any way to set precision.
For a float or a double:
As many digits are written as needed to represent the integral part, followed by the decimal-point character and six decimal digits.


If you use a stringstream instead of a string for f, you have all the usual I/O manipulators you're familiar with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;

int main()
{   float in, po;
    cout << "Enter Variable's Constant: ";
    cin >> in;
    cout << "Enter Variable's Power: ";
    cin >> po;
    while (1)
    {   stringstream f;
        if (in > 0)
            f << "+"; 
        f << setprecision(2) << in; 
        if (po != 0)
        {   f << "x";       
            if (po != 1)
            {   f << "^"; 
                f << po;
            }
        }
        system("cls");
        cout << "Your Current Function: " << f.str() << endl << endl;
        cout << "Enter Variable's Constant: ";
        cin >> in;
        if (in == 0)
            break;
        cout << "Enter Variable's Power: ";
        cin >> po;
    }
    cout << endl;
    system("pause");
    return 0;
}

Last edited on
Topic archived. No new replies allowed.