浮點資料的輸出控制
先說,3.14這類帶小數點常量在c/c++預設為是double型別常量,可以用3.14f來表示float型別常量。
int x =1 整數型變數 整數型常量
int y =1.0 整數型變數 double型常量 表示(執行)的還是整數型別
-------------------------------------------------------------------------------------------------------------------------------------------------------
c++浮點資料的輸出控制(與c不同)
#include<iostream>
#include<Windows.h>
using namespace std;
int main(void){
double value = 12.3456789;
// 預設精度是6,所以輸出為 12.3457
//(預設情況下,精度是指總的有效數字)
cout << value << endl;
// 把精度修改為4, 輸出12.35, 對最後一位四捨五入
// 精度修改後,持續有效,直到精度再次被修改
cout.precision(4);
cout << value << endl;
// 使用定點法, 精度變成小數點後面的位數
// 輸出12.3457
cout.flags(cout.fixed);
cout << value << endl;
// 定點法持續有效
// 輸出3.1416
cout << 3.1415926535 << endl;
// 把精度恢復成有效數字位數,取消定點法
cout.unsetf(cout.fixed);
cout << value << endl; //輸出12.35
cout << 3.1415926535 << endl; //輸出3.142
system("pause");
return 0;
}