关于cout的输出精度?

VonSnowPine 2004-01-04 08:51:56
C中printf("%5.2f\n",num);可以很方便的输出精度(小数点后保留两位小数)。但C++中用cout<<setprecision(5)<<num;输出的是num的宽度。例如用10、100、1000除以3,在C中用5.2f设置,结果为3.33、33.33、333.33;但在C++中用setprecision(5)时却是3.3333、33.333、333.33;有没有办法让它也能像C中设置一样方便。谢谢!
...全文
592 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
kerbcurb 2004-06-13
  • 打赏
  • 举报
回复
cout.precision(20);
cout<<10.0 / 3<<" "<<100.0 / 3<<" "<<1000.0 / 3<<endl;;
daikeshi 2004-05-01
  • 打赏
  • 举报
回复
g++上好像不认cout<<fixed;郁闷ing
gcc/g++ version 2.96
luoxiao_std01 2004-01-22
  • 打赏
  • 举报
回复
各位大大,可否看一下《出鬼了》的贴子!谢了先
CoolDog 2004-01-22
  • 打赏
  • 举报
回复
cout.setf(ios::fixed);//floating-point numbers are not writen in e-notation
cout.setf(ios::showpoint);//a decimal and trailing zeros are always shown
cout.precison(2);
byyyyy 2004-01-22
  • 打赏
  • 举报
回复
// iomanip_setw.cpp
// compile with: /EHsc
// Defines the entry point for the console application.
//
// Sample use of the following manipulators:
// resetiosflags
// setiosflags
// setbase
// setfill
// setprecision
// setw

#include <iostream>
#include <iomanip>

using namespace std;

const double d1 = 1.23456789;
const double d2 = 12.3456789;
const double d3 = 123.456789;
const double d4 = 1234.56789;
const double d5 = 12345.6789;
const long l1 = 16;
const long l2 = 256;
const long l3 = 1024;
const long l4 = 4096;
const long l5 = 65536;
int base = 10;

void DisplayDefault( )
{
cout << endl << "default display" << endl;
cout << "d1 = " << d1 << endl;
cout << "d2 = " << d2 << endl;
cout << "d3 = " << d3 << endl;
cout << "d4 = " << d4 << endl;
cout << "d5 = " << d5 << endl;
}

void DisplayWidth( int n )
{
cout << endl << "fixed width display set to " << n << ".\n";
cout << "d1 = " << setw(n) << d1 << endl;
cout << "d2 = " << setw(n) << d2 << endl;
cout << "d3 = " << setw(n) << d3 << endl;
cout << "d4 = " << setw(n) << d4 << endl;
cout << "d5 = " << setw(n) << d5 << endl;
}

void DisplayLongs( )
{
cout << setbase(10);
cout << endl << "setbase(" << base << ")" << endl;
cout << setbase(base);
cout << "l1 = " << l1 << endl;
cout << "l2 = " << l2 << endl;
cout << "l3 = " << l3 << endl;
cout << "l4 = " << l4 << endl;
cout << "l5 = " << l5 << endl;
}

int main( int argc, char* argv[] )
{
DisplayDefault( );

cout << endl << "setprecision(" << 3 << ")" << setprecision(3);
DisplayDefault( );

cout << endl << "setprecision(" << 12 << ")" << setprecision(12);
DisplayDefault( );

cout << setiosflags(ios_base::scientific);
cout << endl << "setiosflags(" << ios_base::scientific << ")";
DisplayDefault( );

cout << resetiosflags(ios_base::scientific);
cout << endl << "resetiosflags(" << ios_base::scientific << ")";
DisplayDefault( );

cout << endl << "setfill('" << 'S' << "')" << setfill('S');
DisplayWidth(15);
DisplayDefault( );

cout << endl << "setfill('" << ' ' << "')" << setfill(' ');
DisplayWidth(15);
DisplayDefault( );

cout << endl << "setprecision(" << 8 << ")" << setprecision(8);
DisplayWidth(10);
DisplayDefault( );

base = 16;
DisplayLongs( );

base = 8;
DisplayLongs( );

base = 10;
DisplayLongs( );

return 0;
}
byyyyy 2004-01-22
  • 打赏
  • 举报
回复
问一下fixed是什么意思?
byyyyy 2004-01-22
  • 打赏
  • 举报
回复
标准的main()返回值就是int的。
milefo 2004-01-19
  • 打赏
  • 举报
回复
hehe ! xiaocai365也给补充了一下!
VonSnowPine 2004-01-17
  • 打赏
  • 举报
回复
回复的太匆忙,同样也谢谢你,xiaocai365,为什么起这么好玩的名字?还有main()前为什么有int,这样必须在后面加上return 0;?
VonSnowPine 2004-01-05
  • 打赏
  • 举报
回复
谢谢你!阿奈。
sharkhuang 2004-01-05
  • 打赏
  • 举报
回复
顶!
xiaocai365 2004-01-04
  • 打赏
  • 举报
回复
来晚了,补充楼上

#include <iostream>
#include <iomanip>
using namespace std;

// from abitz(阿奈)
//下面借用一下
int main()
{
cout<<fixed<<setprecision(2)
<<1000/3.0<<endl
<<100/3.0<<endl
<<10/3.0<<endl;
}
abitz 2004-01-04
  • 打赏
  • 举报
回复
int main()
{
cout<<fixed<<setprecision(2)
<<1000/3.0<<endl
<<100/3.0<<endl
<<10/3.0<<endl;
}

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧