C++中小数点输出格式(实例代码)
发布时间 - 2026-01-11 01:55:18 点击率:次在《算法竞赛入门经典》一书中

习题1-5 打折 (discount)
一件衣服95元,若消费满300元,可打八五折。输入购买衣服件数,输出需要支付的金额(单位:元),保留两位小数。
我编写的代码为
#include<iostream>
#include<iomanip>
using namespace std;
int main(void){
double s;
cin>>s;
if(s*95>=300){
cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95*0.85<<endl;
}
else{
cout<<setiosflags(ios::fixed)<<setprecision(2)<<s*95<<endl;
}
return 0;
}
于是将C++中如何显示不同格式的小数查了一下:
Floating point output can be changed with << setiosflags( ios::fixed ) : never use scientific notation //绝对不使用科学记数法。 << setiosflags( ios::scientific ) : always use scientific notation //使用科学记数法。 << resetiosflags( ios::floatfield ) : restores default (use fixed or scientific notation based on the precision)//重置为缺省。 << (re)setiosflags( ios::showpoint ) : controls if the trailing zeros and decimal point will be output (default is no) //控制小数点后面的零是否显示。 << setprecision( digits ) : if fixed or scientific format is selected, controls the number of digits after the decimal place, otherwise controls the total number of significant digits// 如果fixed或者scientific已经确定了,则控制小数点后面的位数;否则,控制所有位数。
#include <iostream>
#include <iomanip>
using namespace std;
int main(void){
double S = 0.000000123;
double A = 456.7;
double B = 8910000000000.0;
cout << "default precision is "
<< cout.precision() << endl; // 6
cout << " S = " << S << endl; // 1.23e-07
cout << " A = " << A << endl; // 456.7
cout << " B = " << B << endl; // 8.91e+12
cout << setiosflags(ios::showpoint)
<< "ios::showpoint ON" << endl;
cout << " S = " << S << endl; // 1.23000e-07
cout << " A = " << A << endl; // 456.700
cout << " B = " << B << endl; // 8.91000e+12
cout << resetiosflags(ios::showpoint)
<< "ios::showpoint OFF" << endl;
cout << setiosflags(ios::fixed)
<< "ios::fixed ON" << endl;
cout << " S = " << S << endl; // 0.000000
cout << " A = " << A << endl; // 456.700000
cout << " B = " << B << endl; // 8910000000000.000000
cout << resetiosflags(ios::fixed)
<< setiosflags(ios::scientific)
<< "ios::scientific ON" << endl;
cout << " S = " << S << endl; // 1.230000e-07
cout << " A = " << A << endl; // 4.567000e+02
cout << " B = " << B << endl; // 8.910000e+12
}
输出为:
default precision is 6
S = 1.23e-007
A = 456.7
B = 8.91e+012
ios::showpoint ON
S = 1.23000e-007
A = 456.700
B = 8.91000e+012
ios::showpoint OFF
ios::fixed ON
S = 0.000000
A = 456.700000
B = 8910000000000.000000
ios::scientific ON
S = 1.230000e-007
A = 4.567000e+002
B = 8.910000e+012
--------------------------------
Process exited after 0.02482 seconds with return value 0
请按任意键继续. . .
以上这篇C++中小数点输出格式(实例代码)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
# c
# 输出格式
# C++详解使用floor&ceil&round实现保留小数点后两位
# C++编程中的格式化输出详解
# C++输出问题:保留两位小数
# 给大家
# 两位
# 希望能
# 绝对不
# 这篇
# 请按
# 小编
# 大家多多
# 一书中
# 可打
# 确定了
# 八五折
# void
# main
# cin
# double
# cout
# iomanip
# ios
# namespace
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
Laravel怎么实现API接口鉴权_Laravel Sanctum令牌生成与请求验证【教程】
如何在IIS中新建站点并配置端口与IP地址?
Windows10电脑怎么查看硬盘通电时间_Win10使用工具检测磁盘健康
C++时间戳转换成日期时间的步骤和示例代码
Laravel怎么连接多个数据库_Laravel多数据库连接配置
HTML 中如何正确使用模板变量为元素的 name 属性赋值
Laravel如何正确地在控制器和模型之间分配逻辑_Laravel代码职责分离与架构建议
微信h5制作网站有哪些,免费微信H5页面制作工具?
Python文件异常处理策略_健壮性说明【指导】
Win11摄像头无法使用怎么办_Win11相机隐私权限开启教程【详解】
Laravel如何使用withoutEvents方法临时禁用模型事件
Laravel中的Facade(门面)到底是什么原理
,怎么在广州志愿者网站注册?
北京企业网站设计制作公司,北京铁路集团官方网站?
网站制作报价单模板图片,小松挖机官方网站报价?
做企业网站制作流程,企业网站制作基本流程有哪些?
Laravel怎么集成Log日志记录_Laravel单文件与每日日志配置及自定义通道【详解】
laravel怎么实现图片的压缩和裁剪_laravel图片压缩与裁剪方法
Laravel如何实现本地化和多语言支持_Laravel多语言配置与翻译文件管理
phpredis提高消息队列的实时性方法(推荐)
Python高阶函数应用_函数作为参数说明【指导】
Python自动化办公教程_ExcelWordPDF批量处理案例
如何彻底删除建站之星生成的Banner?
微信小程序 require机制详解及实例代码
如何在万网利用已有域名快速建站?
利用vue写todolist单页应用
Laravel Seeder填充数据教程_Laravel模型工厂Factory使用
Swift开发中switch语句值绑定模式
手机网站制作与建设方案,手机网站如何建设?
Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理
JavaScript 输出显示内容(document.write、alert、innerHTML、console.log)
微博html5版本怎么弄发超话_超话进入入口及发帖格式要求【教程】
,交易猫的商品怎么发布到网站上去?
如何获取PHP WAP自助建站系统源码?
微博html5版本怎么弄发语音微博_语音录制入口及时长限制操作【教程】
Laravel安装步骤详细教程_Laravel环境搭建指南
Laravel软删除怎么实现_Laravel Eloquent SoftDeletes功能使用教程
Win11怎么开启自动HDR画质_Windows11显示设置HDR选项
如何用已有域名快速搭建网站?
Python正则表达式进阶教程_复杂匹配与分组替换解析
Laravel如何使用查询构建器?(Query Builder高级用法)
详解Oracle修改字段类型方法总结
Laravel如何使用Facades(门面)及其工作原理_Laravel门面模式与底层机制
Laravel如何配置中间件Middleware_Laravel自定义中间件拦截请求与权限校验【步骤】
如何用花生壳三步快速搭建专属网站?
免费视频制作网站,更新又快又好的免费电影网站?
Laravel如何监控和管理失败的队列任务_Laravel失败任务处理与监控
Laravel如何处理跨站请求伪造(CSRF)保护_Laravel表单安全机制与令牌校验
浅谈redis在项目中的应用
Firefox Developer Edition开发者版本入口

