ylww

随笔仅作学习记录,请辩证参考!!!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

先说,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;

}

posted on 2022-04-12 19:20  理想主义者光芒万丈  阅读(103)  评论(0)    收藏  举报