I want to remove all the trailing zeros from a double number. I know I can use std::setprecision()
to print it, but I want to modify the actual number to only make it two digits after the decimal.
class Circle {
private:
double r, PI = 3.14159;
public:
Circle(double r) {
this->r = r;
}
double area() {
return PI * r * r;
}
double circumference() {
return 2 * PI * r;
}
double getRadius() {
return r;
}
string toString() {
string str;
str += "Circle{radius=" + to_string((r)) + "}";
return str;
}
};
From the twoString()
function I want to return the radius till two decimal digits only, however, I am getting "Circle{radius=2.500000}" after I pass 2.5, similarly, for 4 I am getting "Circle{radius=4.000000}". I want it to be only 4 here.
Modifying original number won't work because data format for
double
should be defined in your platform (typically IEEE754 format).std::stringstream
should be helpful. It can be used like this: