C++保留小数点后两位(floor&ceil&round)详解

 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;
}

 运行结果

  • 6
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在JS中将数字小数点后两位向上取整,可以使用Math.ceil()函数。该函数会将参数向上取整到最接近的整数。例如,如果要将数字7.23向上取整到小数点后两位,可以使用以下代码:Math.ceil(7.23 * 100) / 100。这将返回7.24,将小数点后两位向上取整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JS小数取整以及保留小数点后两位](https://blog.csdn.net/bide_tootight/article/details/79488973)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [js取小数点后两位的方法](https://blog.csdn.net/cc_yy_zh/article/details/51428702)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [JS取数字小数点后两位或n位的简单方法](https://download.csdn.net/download/weixin_38630139/12991680)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值