C++中提供了四舍五入的函數round(),在cmath的頭文件中,但是四舍五入后小數變為整數,要使保留一位小數可以先乘10,再利用round函數四舍五入,再除以10.0。
代碼如下:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
cout << round(10/8.0*10)/10.0;
cout << "Hello world!" << endl;
return 0;
}