c++里怎么限制输出小数位数
答案:2 悬赏:10
解决时间 2021-03-14 21:09
- 提问者网友:暖心后
- 2021-03-14 10:51
c++里怎么限制输出小数位数
最佳答案
- 二级知识专家网友:错过的是遗憾
- 2021-03-14 12:09
double a=12444.489583484;
CString tmp;
tmp.Format("%7.4f",a);//保留小数点前7位,小数点后4位
若满意请及时采纳。谢谢
CString tmp;
tmp.Format("%7.4f",a);//保留小数点前7位,小数点后4位
若满意请及时采纳。谢谢
全部回答
- 1楼网友:花一样艳美的陌生人
- 2021-03-14 13:24
++ 标准输出如何控制小数点后位数
#include
#include
using namespace std;
int main( void )
{
const double value = 12.3456789;
cout << value << endl; // 默认以6精度,所以输出为 12.3457
cout << setprecision(4) << value << endl; // 改成4精度,所以输出为12.35
cout << setprecision(8) << value << endl; // 改成8精度,所以输出为12.345679
cout << fixed << setprecision(4) << value << endl; // 加了fixed意味着是固定点方式显示,所以这里的精度指的是小数位,输出为12.3457
cout << value << endl; // fixed和setprecision的作用还在,依然显示12.3457
cout.unsetf( ios::fixed ); // 去掉了fixed,所以精度恢复成整个数值的有效位数,显示为12.35
cout << value << endl;
cout.precision( 6 ); // 恢复成原来的样子,输出为12.3457
cout << value << endl;
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯