如何在cout<<中获得十进制后的固定数字
C++ How to get fixed digit after decimal in cout<<
c++如何在输出中获得十进制后的固定数字????像f = 123.456789我想在输出中显示123.46
可以在c++中使用setprecision()方法。
cout<<std::fixed<<std::setprecision(2)<<f;
另一种方法是使用printf函数,确保您包含stdio.h文件来使用它,
printf (" % 0.2 f, f);
这里%f是格式说明符(float), '%'和'f'之间的0.2用于将精度设置为小数点后两位。
您也可以使用boost::format
#include <boost/format.hpp>
cout << boost::format("%.2f") % f << endl;
您需要I/O操纵符来实现十进制精度。
#include <iomanip>
#include <iostream>
int main( )
{
double foo = 123.456789L;
std::cout << std::setprecision(2) << std::fixed << foo << std::endl;
return 0;
}
相关文章:
- 计算十进制 c++ 之后的数字
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何计算整数的十进制数字?
- 使用正则表达式C++计算数字中的十进制空格
- Unicode Chars显示为十进制数字而不是符号,我该如何修复
- 根据参数设置十进制数字
- 将二进制号转换为十进制数字的代码
- 使用动态比特斯转换十进制数字
- 获得数字的十进制部分
- 此代码中 * 1233 >> 12 计算十进制数字背后的数学原理是什么
- 仅使用char将八分音号码转换为十进制数字
- 如何使用C 将二进制数字的字符串转换为其签名的十进制表示
- 努力理解一个以十六进制、十进制、bin 和 dec 计算数字总和的程序
- C 如何将双重转换为字符串,因此它仅在十进制后显示2位数字
- 将十进制数字截断为最接近的4个小数和断言
- 打印长双变量的所有十进制数字
- 十进制数字的区域设置感知编辑控件子类化(格式[sign][xxx..][decimal separator][yy.])
- 反向数字十进制
- 将双精度转换为精度超过 5 位十进制数字的字符串
- 在 cout 中将数字十进制格式显示为指数