當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Math floor()用法及代碼示例

它將值四舍五入到不大於給定值的最接近的整數。

例如:

floor(8.2)=8.0;
floor(-8.8)=-9.0;

用法

假設一個數字是 'x'。語法是:

double floor(double x);

參數

x: 它是四舍五入到最接近的整數的值。

返回值

它返回舍入到不大於 x 的最接近整數的值。

例子1

讓我們看一個簡單的例子,考慮一個正值。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
 float x=7.8;
 std::cout << "Initial value of x is:" << x<<std::endl;
cout<<"Now, the value of x is:"<<floor(x);
 return 0;
}

輸出:

Initial value of x is:7.8
Now, the value of x is:7   

例子2

讓我們看一個考慮負值的簡單例子。

#include <iostream>
#include<math.h>
using namespace std;
int main()
{
 float x=-10.2;
 std::cout << "Initial value of x is:" << x<<std::endl;
cout<<"Now, the value of x is:"<<floor(x);
 return 0;
}

輸出:

Initial value of x is:-10.2
Now, the value of x is:-11  





相關用法


注:本文由純淨天空篩選整理自 C++ Math floor()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。