在c++中使用setprecision
Using setprecision in C++
本文关键字:setprecision c++ 更新时间:2023-10-16
我试图在c++中使用setprecision。这是我正在做的-
我有一个输出文件,我在那里写的结果-
我对function的第一次调用正确地打印了interest_rate。假设我用interest_rate=0.035
调用函数,那么我得到以下输出—
Annual Interest Rate: 0.035
但是下次如果我用相同的interest_rate i.e. 0.035
调用函数,我得到以下输出-
Annual Interest Rate: 0.04
我想在两种情况下得到相同的输出,为什么会发生这种情况?
编辑:-下面是代码-
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
输入文件:- input.txt
3
1000.00
0.035
120.00
5000.00
0.04
165.00
200000.00
0.055
2250.00
下面是我得到的输出文件:
Annual Interest Rate: 0.035
1 1000.00
2 1000.00
3 1000.00
4 1000.00
Annual Interest Rate: 0.04
1 5000.00
2 5000.00
3 5000.00
4 5000.00
Annual Interest Rate: 0.06
1 200000.00
2 200000.00
3 200000.00
4 200000.00
注意输入文件中的0.055如何在输出文件
一些流操纵符/标志只应用一次(例如:setw
),而其他的在它们被改变之前保持有效(例如:fixed
和setprecision
)。
我假设,在函数的第一次调用中,您使用的是流的默认格式(不是fixed
也不是scientific
)和默认精度(6)。
在函数的第二次调用中(我假设您没有在另一个函数中同时更改它),格式为fixed
,精度为2。
相关文章:
- 在c++中使用setw和setprecision时,如何显示带值的$符号
- 在 c++ 中,没有 **std::fixed** 的 **std::setprecision()** 的作用是什么?
- 正确设置"setprecision"
- 在处理C 中的数字格式时,在SetPrecision中使用固定关键字
- C 操纵器,可以将SetPrecision Manip与SetFill Manip相结合
- 循环中的setPrecision
- 为什么 std::setprecision 显示不存在的精度位以及如何查看实际浮点数
- SetPrecision()无法正常工作
- 为什么 MSVC++ 需要 std::setprecision 才能使用 std::hexfloat 启用全精度输出
- 如何在 c++ 中将 std:fixed << setprecision(5) 的结果分配给变量
- 我可以将 std:fixed 或 std::setprecision() 与>>运算符一起使用吗?
- STD :: SetPrecision不显示尾随的小数
- 为什么 cout << setprecision(2) << 0.999 的输出是 1 而不是 1.0?
- 为什么 std::setprecision(6) 在固定宽度模式式传输超过 6 位数字
- 为什么 setprecision( ) 在一个数字中给我两个单独的小数点
- 文件SetPrecision C 代码
- “setprecision”不是“std”的成员
- iOS::Base:Precision & SetPrecision
- 为什么 setPrecision 不自动使用最小可用精度?
- 如何正确使用 setprecision() 将".00"添加到金额的末尾?