網(wǎng)站首頁 編程語言 正文
C++詳解使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位_C 語言
作者:IT.Husky ? 更新時(shí)間: 2022-08-23 編程語言C++四舍五入保留小數(shù)點(diǎn)后兩位
示例
#include <iostream>
using namespace std;
int main()
{
double i = 2.235687;
double j = round(i * 100) / 100;
cout << "The original number is " << i << endl;
cout << "The keep two decimal of 2.235687 is " << j << endl;
system("pause");
return 0;
}
運(yùn)行結(jié)果
函數(shù)解析見下面
1、floor函數(shù)
功能:把一個(gè)小數(shù)向下取整? ? ? 即就是如果數(shù)是2.2,那向下取整的結(jié)果就為2.000000
原型:double floor(doube x);
? ? 參數(shù)解釋:
? ? ? ? x:是需要計(jì)算的數(shù)
示例
#include <iostream>
using namespace std;
int main()
{
double i = floor(2.2);
double j = floor(-2.2);
cout << "The floor of 2.2 is " << i << endl;
cout << "The floor of -2.2 is " << j << endl;
system("pause");
return 0;
}
運(yùn)行結(jié)果
2、ceil函數(shù)
功能:把一個(gè)小數(shù)向上取整
? ? ? 即就是如果數(shù)是2.2,那向下取整的結(jié)果就為3.000000
原型:double ceil(doube x);
? ? 參數(shù)解釋:
? ? ? ? x:是需要計(jì)算的數(shù)
示例
#include <iostream>
using namespace std;
int main()
{
double i = ceil(2.2);
double j = ceil(-2.2);
cout << "The ceil of 2.2 is " << i << endl;
cout << "The ceil of -2.2 is " << j << endl;
system("pause");
return 0;
}
運(yùn)行結(jié)果
3、round函數(shù)
功能:把一個(gè)小數(shù)四舍五入? ? ? 即就是如果數(shù)是2.2,那向下取整的結(jié)果就為2? ? ? ? ? ? ? ???如果數(shù)是2.5,那向上取整的結(jié)果就為3
原型:double round(doube x);
? ? 參數(shù)解釋:
? ? ? ? x:是需要計(jì)算的數(shù)
示例
#include <iostream>
using namespace std;
int main()
{
double i = round(2.2);
double x = round(2.7);
double j = round(-2.2);
double y = round(-2.7);
cout << "The round of 2.2 is " << i << endl;
cout << "The round of 2.7 is " << x << endl;
cout << "The round of -2.2 is " << j << endl;
cout << "The round of -2.7 is " << y << endl;
system("pause");
return 0;
}
運(yùn)行結(jié)果
原文鏈接:https://blog.csdn.net/Gary_ghw/article/details/125498414
相關(guān)推薦
- 2022-06-08 報(bào)錯(cuò):No fallback instance of type class**解決辦法
- 2023-01-26 Android?源碼淺析RecyclerView?Adapter_Android
- 2022-09-23 WPF仿微信實(shí)現(xiàn)截圖功能的方法詳解_C#教程
- 2022-02-20 小程序獲取今天,昨天,前天,上月,上周時(shí)間全
- 2022-11-27 Unity?數(shù)據(jù)存儲(chǔ)和讀取的方法匯總_C#教程
- 2022-10-13 解析批處理命令call和start_DOS/BAT
- 2022-04-04 HTML頁面在ios上滾動(dòng)卡頓的解決方案
- 2022-12-31 C++淺析序列數(shù)據(jù)封裝與優(yōu)化實(shí)現(xiàn)方法_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支