日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(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

欄目分類
最近更新