C++詳解使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位
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ù)解析見(jiàn)下面
1、floor函數(shù)
功能:把一個(gè)小數(shù)向下取整 即就是如果數(shù)是2.2,那向下取整的結(jié)果就為2.000000
原型:double floor(doube x);
參數(shù)解釋?zhuān)?br /> 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ù)解釋?zhuān)?br /> 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ù)解釋?zhuān)?br /> 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é)果

到此這篇關(guān)于C++詳解使用floor&ceil&round實(shí)現(xiàn)保留小數(shù)點(diǎn)后兩位的文章就介紹到這了,更多相關(guān)C++ floor ceil round內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c語(yǔ)言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具代碼分享
這篇文章主要介紹了c語(yǔ)言實(shí)現(xiàn)系統(tǒng)時(shí)間校正工具,大家參考使用吧2014-01-01
C語(yǔ)言結(jié)構(gòu)體中內(nèi)存對(duì)齊的問(wèn)題理解
內(nèi)存對(duì)齊”應(yīng)該是編譯器的“管轄范圍”。編譯器為程序中的每個(gè)“數(shù)據(jù)單元”安排在適當(dāng)?shù)奈恢蒙?。但是C語(yǔ)言的一個(gè)特點(diǎn)就是太靈活,太強(qiáng)大,它允許你干預(yù)“內(nèi)存對(duì)齊”。如果你想了解更加底層的秘密,“內(nèi)存對(duì)齊”對(duì)你就不應(yīng)該再模糊了2022-02-02
C++?反匯編之關(guān)于Switch語(yǔ)句的優(yōu)化措施
這篇文章主要介紹了C++?反匯編之關(guān)于Switch語(yǔ)句的優(yōu)化措施,利用三種優(yōu)化來(lái)降低樹(shù)高度,誰(shuí)的效率高就優(yōu)先使用誰(shuí),三種優(yōu)化都無(wú)法匹配才會(huì)使用判定樹(shù),具體內(nèi)容詳情跟隨小編一起看看吧2022-01-01
C語(yǔ)言實(shí)現(xiàn)發(fā)送郵件功能
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)發(fā)送郵件功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
C++多字節(jié)字符與寬字節(jié)字符相互轉(zhuǎn)換
最近在C++編程中經(jīng)常遇到需要多字節(jié)字符與寬字節(jié)字符相互轉(zhuǎn)換的問(wèn)題,自己寫(xiě)了一個(gè)類(lèi)來(lái)封裝wchar_t與char類(lèi)型間的轉(zhuǎn)換2012-11-11
關(guān)于C++讀入數(shù)字按位取出與進(jìn)制轉(zhuǎn)換問(wèn)題(典型問(wèn)題)
這篇文章主要介紹了關(guān)于C++讀入數(shù)字按位取出與進(jìn)制轉(zhuǎn)換問(wèn)題,是一個(gè)非常典型的問(wèn)題,本文通過(guò)實(shí)例舉例給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-02-02

