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


C++ llround()用法及代碼示例

C++ 中的llround() 函數對最接近參數的整數值進行四舍五入,中間情況從零四舍五入。

C++ 中的llround() 函數對最接近參數的整數值進行四舍五入,中間情況從零四舍五入。返回值的類型為 long long int. 它類似於 lround() 函數,但返回 long long int 而 lround 返回 long int.

llround() 原型 [從 C++ 11 標準開始]

long long int llround(double x);
long long int llround(float x);
long long int llround(long double x);
long long int llround(T x); // For integral type

llround() 函數采用單個參數並返回 long long int. 該函數在 <cmath> 頭文件中定義。

參數:

llround() 函數采用單個參數值進行舍入。

返回:

llround() 函數返回最接近 x 的整數值,中間情況從零四舍五入。返回值的類型為 long long int.

示例 1:llround() 如何在 C++ 中工作?

#include <iostream>
#include <cmath>

using namespace std;

int main()
{   
    long long int result;
    double x = 11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    x = 13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = 50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -11.16;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    x = -13.87;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    x = -50.5;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;
    
    return 0;
}

運行程序時,輸出將是:

llround(11.16) = 11
llround(13.87) = 14
llround(50.5) = 51
llround(-11.16) = -11
llround(-13.87) = -14
llround(-50.5) = -51

示例 2:用於整數類型的 llround() 函數

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int x = 15;
    long long int result;
    result = llround(x);
    cout << "llround(" << x << ") = " << result << endl;

    return 0;
}

運行程序時,輸出將是:

llround(15) = 15

對於整數值,應用 llround 函數返回與輸入相同的值。所以在實踐中它並不常用於整數值。

相關用法


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