四舍五入到特定的小数位置

来源:互联网 发布:snh48 team sii知乎 编辑:程序博客网 时间:2024/06/05 20:00

使用floor函数把数字舍入到特定的小数位置
0.05舍入到十分位为0.1;
0.005舍入到百分位为0.01;

#include <iostream>#include <cmath>using namespace std;int main(){    double n;    int point;    cout << "输入数字:";    cin >> n;    cout << "输入舍入的小数位数,如舍入到百分位,输入2,";    cin >> point;    int temp = pow(10, point);    cout << floor(n*temp + 0.5) / temp;    system("pause");    return 0;}
0 0
原创粉丝点击