请问c++ cout 格式 的问题在c++里 如果 想cout 一个小数,怎么规定他的小数位数呢?
eg:
num = 3.6
cout<<num;
只能得到3.6
如果想得到3.60 怎么写呢?
谢谢!!![hobo (3-4 10:49, Long long ago)]
[ 传统版 |
sForum ][登录后回复]1楼
answer:#include <iostream.h>
#include <iomanip.h>
int main()
{
double num = 3.6;
cout << setiosflags(ios::fixed | ios::showpoint);
cout << setprecision(2);
cout << num << endl;
}
/*
refer to any iomanip formatting document for c++ io formatting
*/[simon (3-4 11:31, Long long ago)]
[ 传统版 |
sForum ][登录后回复]2楼
(引用 simon:answer:#include
#include
int main()
{
double num = 3.6;
cout ...)Complement =>The output will be 3.6 if you use
cout << setiosflags(ios::showpoint) << setprecision (2) << num
<< endl;
And output will be 3.60 if you use
cout << setiosflags(ios::fixed) << setprecision(2) << num
<< endl;
if you use cout << setiosflags(ios::fixed||ios::showpoint)
<< setprecision(2) << num << endl; , the output will be
3.60.
[魅力十足 (3-5 0:04, Long long ago)]
[ 传统版 |
sForum ][登录后回复]3楼