English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

تعليمية C++ الأساسية

C++ 流程控制

الوظائف C++

C++ 数组 & 字符串

C++ 数据结构

C++ 类 & 对象

الإشارات C++

الوراثة C++

تعليمية STL C++

C++ 参考手册

C++ llround() 函数使用方法及示例

وظائف المكتبة <cmath>

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); //为整型

llround()函数采用单个参数,并返回long long int类型的值。此函数在头文件中定义。

llround()参数

llround()函数将单个参数值取整。

llround()返回值

llround()函数返回最接近x的整数值,中间情况下从零舍入。返回的值是long long int类型。

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

#include 
#include 
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 
#include 
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 القيمة نفسها. لذلك، لا تستخدم عادة لتعريف القيم الزائدة.

وظائف المكتبة <cmath>