c++ setprecision用法详解
下面的程序显示了用不同数量的有效数来显示除法运算的结果:
// This program demonstrates how the setprecision manipulator // affects the way a floating-point value is displayed. #include iostream #include iomanip // Header file needed to use setprecision using namespace std; int main() double number1 = 132.364, number2 = 26.91; double quotient = number1 / number2; cout quotient endl; cout setprecision(5) quotient endl; cout setprecision(4) quotient endl; cout setprecision(3) quotient endl; cout setprecision(2) quotient endl; cout setprecision(1) quotient endl; return 0; }
程序输出结果:
4.91877
4.9188
4.919
4.92
4.9
5
注意,使用预标准编译器输出的结果可能与此结果不同。
程序中的第一个值显示在第 11 行,没有设置 setprecision 操作符(默认情况下,系统使用 6 个有效数显示浮点值)。后续的 cout 语句打印相同的值,但四舍五入为 5、4、3、2 和 1 个有效数。
请注意,与 setw 不同的是,setprecision 不计算小数点。例如,当使用 setprecision(5) 时,输出包含 5 位有效数,但是需要 6 个位置来显示 4.9188。
如果一个数字的值可以由少于 setprecision 指定的精度位数来表示,则操作符将不起作用。在以下语句中,dollars 的值只有 4 位数字,所以 2 个 cout 语句显示的数字都是 24.51:
double dollars = 24.51;
cout dollars endl; // 显示 24.51
cout setprecision (5) dollars endl; // 显示 24.51
表 1 显示了 setprecision 如何影响各种值的显示方式。请注意,如果有效数少于要显示的数字,则 setprecision 将舍入,而不是截断数字。另外还需要注意的是,末尾的零将被省略。因此,尽管指定了 setprecision(5),但是 21.40 仍显示为 21.4。
与 setw 字段宽度不同的是,setprecision 的精度设置将保持有效,直到更改为其他值为止。与所有格式化操作符一样,必须包含头文件 iomanip 才能使用 setprecision。
下面的程序显示了如何组合使用 setw 和 setprecision 操作符来控制显示浮点数的方式。
// This program asks for sales figures for three days. // The total sales are calculated and displayed in a table. #include iostream #include iomanip // Header file needed to use stream manipulators using namespace std; int main() double day1, day2, day3, total; // Get the sales for each day cout Enter the sales for day 1: cin dayl; cout Enter the sales for day 2: cin day2; cout Enter the sales for day 3: cin day3; // Calculate total sales total = day1 + day2 + day3; // Display the sales figures cout /nSales Figures/n cout -------------/n cout setprecision (5); cout Day 1: setw(8) day1 endl; cout Day 2: setw(8) day2 endl; cout Day 3: setw(8) day3 endl; cout Total: setw(8) total endl; return 0; }
程序输出结果:
Enter the sales ;for day 1: 321.57 Enter the sales for day 2: 269,60 Enter the sales for day 3: 307.00 Sales Figures ------------- Day 1: 321.57 Day 2: 269.6 Day 3: 307 Total: 898.17
该程序创建的输出,按照指示,允许显示最多 5 个有效数,并以 8 个字符的字段宽度右对齐打印。
22084.html
html相关文章
- C++中list用法详解[通俗易懂]
- C++之constexpr详解
- 排序算法的实现(C/C++实现)详解编程语言
- C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)编程语言
- C++11 std::shared_ptr总结与使用详解编程语言
- C++11 多线程编程 使用lambda创建std::thread (生产/消费者模式)详解编程语言
- C++函数的高级特性详解编程语言
- VS Code 编译C++详解编程语言
- php和c++自带的排序算法详解编程语言
- C++ uniform_real_distribution连续均匀分布类模板用法详解
- C++析构函数详解
- C++ pair(STL pair)类模板的用法详解
- C++ set添加、删除和访问(STL set添加、删除和访问)元素详解
- C++ set_intersection(STL set_intersection)用法详解
- C++ partition_copy(STL partition_copy)算法使用详解
- C++ valarray用法(对象初始化和成员函数)详解
- C++ const用法详解
- C++ left和right操作符用法详解
- C++ cin.getline用法详解
- C++左值和右值(详解版)
- C++ tellg和tellp函数用法详解
- C++类模板用法详解
- C++ STL流迭代器(stream_iterator)用法详解
- C++十六进制宏的用法详解
- C++中静态存储区与栈以及堆的区别详解
- VC++中的字体设置方法详解