C++ Math lrint()函数

C++ Math lrint()函数

该函数根据当前的舍入模式将给定的值四舍五入,并返回 long int 类型的值。

语法

假设一个数为 ‘x’,语法如下:

long int lrint(data_type x);

参数

x :可以是浮点数、双精度浮点数或长双精度浮点数的值。

返回值

它返回 x 的四舍五入值,返回值的类型是长整型。

示例1

让我们看一个简单的示例。

#include 
#include
#include 
using namespace std;
int main()
{
    float r;
    int str;
    cout<<"Enter the value which you want to round :";
    std::cin >> r ;
    cout<<'\n';
    cout<<"Name of the methods are :"<<'\n'<<"1. Rounding downwards"<<'\n'<<"2. Rounding upwards"<<'\n'<<"3. 
    Rounding towards zero"<<'\n'<<"4. Rounding to the nearest"<<'\n';
    cout<<"Enter the number of rounding method :";
    cin>>str;
    cout<<'\n';
    switch(str)
    {
        case 1:
        fesetround(FE_DOWNWARD);
cout<<"Rounding downwards,value is :"<

输出:

Enter the value which you want to round : 7.8

Name of the methods are :
1. Rounding downwards
2. Rounding upwards
3. Rounding towards zero
4. Rounding to the nearest
Enter the number of rounding method : 1

Rounding downwards,value is :7

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程