C++实现round四舍五入四位小数,类似C#的round函数
C++实现round四舍五入N位小数
#include <iostream> #include <sstream>
#include <iomanip>
using namespace std;
double round(double number, unsigned int bits) {
stringstream ss;
ss << fixed << setprecision(bits) << number;
ss >> number;
return number;
}
int main() {
double number = 3.1415926535897932;
cout << fixed << showpoint << setprecision(15);
cout << "一开始number = " << number << endl;
for (int i = 0; i < 15; ++i) {
cout << "number保留" << i << "位小数后为: "
<< round(number, i) << endl;
}
return 0;
}
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。