首页 > 其他分享 >关于cout << fixed << setprecision(2)

关于cout << fixed << setprecision(2)

时间:2023-02-18 16:14:00浏览次数:36  
标签:输出 cout 输出精度 setprecision 浮点数 fixed

fixed:

用于设置浮点数的输出精度为固定小数位数(6位)。

只要出现了fixed,则后面都是以fixed输出(就是说,如果之后还要继续使用,不用再打一遍fixed了),如果取消使用 unsetf 函数。

 

setprecision(2):

用于设置浮点数的输出精度为小数点后两位。

 

∴cout << fixed << setprecision(2)用于控制浮点数输出的精度,保留两位小数。

 

例如:

double x = 1.203;
cout << fixed << setprecision(5) << x;

输出为:

 

标签:输出,cout,输出精度,setprecision,浮点数,fixed
From: https://www.cnblogs.com/marswithme/p/17132849.html

相关文章