c++控制小数点后位数

2024-12-05 06:38:06
推荐回答(3个)
回答1:

<< fixed<
看看下面的程序:
#include
#include

using namespace std;

const double d1 = 1.23456789;
const double d2 = 12.3456789;
const double d3 = 123.456789;
const double d4 = 1234.56789;
const double d5 = 12345.6789;

int main( int argc, char* argv[] )
{
cout << "d1 = " << fixed< cout << "d2 = " << fixed< cout << "d3 = " << fixed< cout << "d4 = " << fixed< cout << "d5 = " << fixed<
cout<
cout << "d1 = " << fixed< cout << "d2 = " << fixed< cout << "d3 = " << fixed< cout << "d4 = " << fixed< cout << "d5 = " << fixed<
return 0;
}

回答2:

如0.4%f即可及 后有四位

回答3:

你可以这样:int i;
cin>>i;
i*=1.0000;
cout<不过这样更简单:
cout<cout<就可以显示4位小数.
两种方法都可以的,我已试过了,其实就是一个简单的问题