
C++进行特定位数的四舍五入/向上取整/向下取整/截断
1.C++的四舍五入/向上取整/向下取整/截断
我们在C++中有时会需要使用到数学运算,同样的可能会使用到标题提到的几种方式进行运算。实际上C++有一个库可以直接实现这些方法操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <cmath>
int main() { double a = 43.5555;
a = std::round(a); std::cout << a << std::endl; a = std::floor(a); std::cout << a << std::endl;
a = std::ceil(a); std::cout << a << std::endl; a = std::trunc(a); std::cout << a << std::endl;
return 0; }
|
那么这就是最简单的四种算法在C++中的使用了。
2.C++中获取特定位数的方法
那么有时候我们进行这样的数学运算后,目的就是为了得到一个更方便看的数,那当然要取特定位数的小数了。写法规则如下(以四舍五入方法std::round做演示)。
1
| a = std::round(a * 保留到几分位) / 保留到几分位;
|
我们来一个保存两位小数(十分位)的实例来看一下。
1 2 3 4 5 6 7 8 9 10 11
| #include <iostream> #include <cmath>
int main() { double a = 43.5555; a = std::round(a * 10) / 10; std::cout << a << std::endl;
return 0; }
|
那么这样就可以非常轻松的在C++中获取到我们需要的小数位数了。
3.C++中对定位数的double进行输出的方法
我们上面说到了如何获取到特定的位数,但是这样获取到的还是double值,如果想要和std::string一起操作是会出现问题的。如果我们想要把这个double和其他的string放到一起的话,我们就需要按照如下代码进行操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <iostream> #include <cmath> #include <string>
int main() { double a = 43.5555; a = std::round(a * 10) / 10; std::string tempre = std::to_string(a); std::cout << tempre.substr(0, tempre.find(".") + 2) << std::endl;
return 0; }
|
这就是本文的全部内容了。