如何输出计算到小数点后两位的中间器?
How to output an interger which is calculated to two decimal places?
很容易输出一个双精度值,该值计算到小数点后两位。 代码片段如下:
cout.setf(ios_base::showpoint);
cout.setf(ios_base::fixed, ios_base::floatfield);
cout.precision(2);
cout << 10000000.2 << endl; // output: 10000000.20
cout << 2.561452 << endl; // output: 2.56
cout << 24 << endl; // output: 24 but I want 24.00, how to change my code?
如何输出计算到小数点后两位的中间器?我想要 24.00 作为输出。
这取决于你的 24 是什么。
如果是硬编码值,则只需编写:
std::cout << 24.00 << std::endl;
如果是整数变量,请编写以下内容:
std::cout << static_cast<double>(myIntegerVariable) << std::endl;
不要使用任何建议的方法,例如添加".00",因为如果您以后想更改精度,这会破坏您的代码。
重写完整性,请尝试以下
#include <iostream>
#include <iomanip>
int main()
{
int i = 24;
std::cout << std::fixed << std::setprecision(2) << double(i) << std::endl;
// Output: 24.00
}
相关文章:
- 在两个线程上读/写 64 位,无互斥/锁定/原子
- 如何在C ++中替换整数的前两位数字?
- 如何输出计算到小数点后两位的中间器?
- 将两位数字转换为低内存表示的最快方法
- 如何在计算器中将大数字表示为小数点后两位C++
- 可视化 如何使用C++向右强制小数点后两位
- 四舍五入双小数点后两位
- 将浮点数限制为小数点后两位
- C++ 如何获取兰登数以及如何保存带有 ONY 两位小数的双精度
- 使用 "double" 输出具有两位小数的值
- 程序查找并打印第一个完全正方形 (i*i),其最后两位数字都是奇数
- 转换为十六进制并删除最后两位数字
- 我将如何在不使用 if 语句、字符数组、apstring 或 atoi 的情况下将带有两位数字的字符串转换为 int
- C++四舍五入小数点后两位
- 不使用64位中间数的32位乘法
- 如何正确格式化c++浮点数到小数点后两位
- 四舍五入浮点数:精确地输出小数点后的两位数字
- 我在试着编写一种疾病在不同年龄段的发病率,我需要把它写成一个百分数它的精度是小数点后两位
- 如何将双精度转换为小数点后两位
- 将字节转换为两位十六进制数字