格式化小数位 [英] Formatting decimal places

查看:93
本文介绍了格式化小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试格式化输出以显示设定数量的小数

个地方。我一直在尝试使用< iomanip> setprecision(),但

只显示总位数。有人可以请

帮帮我????谢谢。

I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.

推荐答案

Uzytkownik" Andrew" <广告***** @ hqcnsg.navy.mil> napisal w wiadomosci

新闻:8e ************************** @ posting.google.c om ...
Uzytkownik "Andrew" <ad*****@hqcnsg.navy.mil> napisal w wiadomosci
news:8e**************************@posting.google.c om...
我试图格式化我的输出以显示一定数量的小数
位置。我一直在尝试使用< iomanip> setprecision(),但
只显示总位数。有人可以帮助我吗????谢谢。
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.




查看boost :: format。


Marcin



Check out boost::format.

Marcin




" Andrew" <广告***** @ hqcnsg.navy.mil>在消息中写道

news:8e ************************** @ posting.google.c om ...

"Andrew" <ad*****@hqcnsg.navy.mil> wrote in message
news:8e**************************@posting.google.c om...
我试图格式化我的输出以显示一定数量的小数
位置。我一直在尝试使用< iomanip> setprecision(),但
只显示总位数。有人可以帮助我吗????谢谢。
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.




你必须将''浮动模式''设置为固定。例如


#include< iostream>


#include< iomanip>


使用namespace std;


int main()


{


cout<<固定<< setprecision(1)<< 1.23456789<< ''\ n'';


cout<<固定<< setprecision(2)<< 1.23456789<< ''\ n'';


cout<<固定<< setprecision(3)<< 1.23456789<< ''\ n'';


cout<<固定<< setprecision(4)<< 1.23456789<< ''\ n'';


cout<<固定<< setprecision(5)<< 1.23456789<< ''\\ n'';


}


john




You have to set the ''float mode'' to fixed. E.g.

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

cout << fixed << setprecision(1) << 1.23456789 << ''\n'';

cout << fixed << setprecision(2) << 1.23456789 << ''\n'';

cout << fixed << setprecision(3) << 1.23456789 << ''\n'';

cout << fixed << setprecision(4) << 1.23456789 << ''\n'';

cout << fixed << setprecision(5) << 1.23456789 << ''\n'';

}

john



Marcin Kalicinski写道:
Marcin Kalicinski wrote:

Uzytkownik" Andrew" <广告***** @ hqcnsg.navy.mil> napisal w wiadomosci
新闻:8e ************************** @ posting.google.c om ...

Uzytkownik "Andrew" <ad*****@hqcnsg.navy.mil> napisal w wiadomosci
news:8e**************************@posting.google.c om...
我试图格式化我的输出以显示一定数量的小数
位置。我一直在尝试使用< iomanip> setprecision(),但
只显示总位数。有人可以帮助我吗????谢谢。
I trying to format my output to display a set number of decimal
places. I have been trying to use the <iomanip> setprecision(), but
that will only display the total number of digits. Can someone please
help me???? Thanks.



查看boost :: format。

Marcin



Check out boost::format.

Marcin




Boost是OT in这个论坛。



Boost is OT in this forum.


这篇关于格式化小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆