删除c++中小数点后的数字(不带floor()函数)

Remove numbers after decimal point in c++ (without floor() function)

本文关键字:函数 不带 floor 数字 c++ 小数点 删除      更新时间:2023-10-16

如果不使用floor()函数,我将如何复制下面的代码?我需要这样做,因为我的职位不能使用floor()功能

double quickExampleee = 3.1459038585;
std::cout << std::floor(quickExamplee  * 100.) / 100. << std::endl; 

我到处找答案,却什么都找不到?无论如何,感谢您抽出时间,

如果您不喜欢将数字强制转换为int,比如,那么使用std::setprecision怎么样

std::cout << std::setprecision(0) << quickExamplee << std::endl;

所以要打印出3.14,只需将精度设置为2,就像一样

std::cout << std::setprecision(2) << quickExamplee << std::endl;

只要双精度大于0,就可以执行static_cast<int>(quickExampleee * 100.)

int myFloorForWhateverReason(double x) {
    if (x > 0.0)
        return (int)x;
    else
        return (int)(x - 0.5);
}
double quickExampleee = 3.1459038585;
std::cout << myFloorForWhateverReason(quickExamplee  * 100.) / 100. << std::endl;