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


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

該函數使用當前舍入方法將給定值舍入到附近的整數值。當前的舍入方法由fegetround 描述。

用法

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

return_type nearbyint(data_type x);

注意:return_type 可以是 float、double 或 long double。

參數

x:值可以是浮點數或雙精度數。

返回值

它使用舍入方法將 x 的舍入值返回到附近的整數值。

例子1

讓我們看一個簡單的例子。

#include <iostream>
#include<math.h>
#include <cfenv>
using namespace std;
int main()
{
     float x=2.3;
    std::cout << "Value of x is:" <<x<< std::endl;
    cout<<"Rounding to nearby,value is:"<<nearbyint(x);
    return 0;
}

輸出:

Value of x is:2.3
Rounding to nearby,value is:2   





相關用法


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