问题描述
我想从双精度数中删除所有尾随零。我知道我可以使用std::setprecision()
来打印它,但是我想修改实际数字,使其仅使小数点后两位数字。
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;
}
};
从twoString()
函数中,我只想返回半径,直到两位小数位,但是,经过2.5之后,我得到的是“ Circle {radius = 2.500000}”,同样,对于4,我得到的是“ Circle {radius = 4.000000}”。我希望这里只有4个。
解决方法
修改原始编号将不起作用,因为source /home/sasha/anaconda3/bin/activate
conda activate gl-env (gl-env is my virtual environment)
pip3 install jupyter notebook
pip3 install matplotlib
pip3 install -U turicreate
jupyter notebook
的数据格式应在您的平台中定义(通常为IEEE754格式)。
std::stringstream
应该会有所帮助。可以这样使用:
double
,
或者您可以:
rad = (r * 100) / 100;
,然后在打印方法中使用rad代替r。这会将其截断到小数点后两位。
如果要四舍五入,请使用
rad = ceilf(r * 100) / 100;