網站首頁 編程語言 正文
C++四舍五入保留小數點后兩位
示例
#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;
}
運行結果
函數解析見下面
1、floor函數
功能:把一個小數向下取整? ? ? 即就是如果數是2.2,那向下取整的結果就為2.000000
原型:double floor(doube x);
? ? 參數解釋:
? ? ? ? x:是需要計算的數
示例
#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;
}
運行結果
2、ceil函數
功能:把一個小數向上取整
? ? ? 即就是如果數是2.2,那向下取整的結果就為3.000000
原型:double ceil(doube x);
? ? 參數解釋:
? ? ? ? x:是需要計算的數
示例
#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;
}
運行結果
3、round函數
功能:把一個小數四舍五入? ? ? 即就是如果數是2.2,那向下取整的結果就為2? ? ? ? ? ? ? ???如果數是2.5,那向上取整的結果就為3
原型:double round(doube x);
? ? 參數解釋:
? ? ? ? x:是需要計算的數
示例
#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;
}
運行結果
原文鏈接:https://blog.csdn.net/Gary_ghw/article/details/125498414
相關推薦
- 2022-05-11 解決 IntelliJ IDEA 中 .propertise 文件保存后中文亂碼
- 2022-10-01 Python中的閉包使用及作用_python
- 2022-07-06 pyinstaller?pathex參數引發打包no?module?name異常_python
- 2022-11-04 詳解C++?指針與二維數組名_C 語言
- 2023-03-27 基于Unity3D實現仿真時鐘詳解_C#教程
- 2022-06-30 Oracle對PL/SQL中的異常處理_oracle
- 2021-12-13 C語言魔方陣的三種實現方法_C 語言
- 2023-07-25 使用SpringBoot+JWT+MybatisPlus實現簡單的登陸狀態驗證
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支