C ++:いくつかの小数点以下の桁数の後

いくつかの単語の男は、コードに直接、言いました

	#include <iomanip>  //不要忘了头文件
	
	#define PI acos(-1)
	
    cout << PI << endl;  //3.14159
    double num = 0.202044;
//第一种写法:设置一次,即可生效
	cout << setiosflags(ios::fixed) << setprecision(8)<<PI<<endl; //3.14159265
    cout << num << endl;    //0.20204400                                                       
//第二种写法:设置一次,即可生效
	cout.setf(ios::fixed);
	cout<<setprecision(8)<<PI<<endl;   //3.14159265
	cout << num << endl;              //0.20204400 
//第三种写法:设置一次,即可生效
	cout << fixed << setprecision(8) << PI << endl;  //3.14159265
	cout << num << endl;                             //0.20204400 

つ、てSetPrecision(N)

機能:浮動表示コントロール有效数字番号を。

  1. 私は一度だけ書くことができます
  2. 単に丸い修正デジタル表示方法、元の番号は変更しません。12.212144数字自体が変化した場合、それは、有効数字2桁が4桁に変更し、有効数字2桁から、その後、12に変更されては、12.00の代わりに、12.21となります。
  3. あなたはあまりにも多くを保持したい場合は、それがアップすることはありません0
  4. 小数点の前の桁数は、より多くのあなたが残しておきたいビット数よりも、科学的表記法を使用する場合
#include   <iostream>
#include <iomanip> 

using namespace std; 

int main() {

	double num = 12.212144;

	cout << setprecision(2) << num << endl;
	cout << num << endl;
	cout << setprecision(4) << num << endl;
	cout << setprecision(6) << num << endl;
	cout << setprecision(7) << num << endl;
	cout << setprecision(8) << num << endl;
	cout << setprecision(10) << num << endl;
	cout << setprecision(1) << num << endl;

	return 0;
}
//结果:
12
12
12.21
12.2121
12.21214
12.212144
12.212144
1e+01
  1. 最後に小数点が0の場合、それは最後に表示されません。
#include   <iostream>
#include <iomanip> 

using namespace std; 

int main() {

	double num = 12.202044;

	cout << setprecision(2) << num << endl;
	cout << num << endl;
	cout << setprecision(4) << num << endl;
	cout << setprecision(6) << num << endl;
	cout << setprecision(7) << num << endl;
	cout << setprecision(8) << num << endl;
	cout << setprecision(10) << num << endl;
	cout << setprecision(1) << num << endl;
		
	return 0;
}
//结果
		12
		12
		12.2
		12.202
		12.20204
		12.202044
		12.202044
		1e+01

三、固定

SetPrecision(n)といくつかの単語の固定された組み合わせは、小数点以下に制御することができます。
ちょうどそれで次の文のいずれかを追加します。

cout<<setiosflags(ios::fixed);

cout.setf(ios::fixed);

cout<<fixed;

固定ビット数は、元の小数点数よりもより多くなるように保持される場合、0がいっぱいになります。
使用方法は、一部を参照してください。

参考:
第1条
の侵略削除されました

リリース3元の記事 ウォンの賞賛0 ビュー245

おすすめ

転載: blog.csdn.net/weixin_43991826/article/details/105306706