round(x)函数详解

首页 / C++入门教程 / round(x)函数详解

此函数用于舍入给定值,该值可以是float或double。

例如:

round(5.8)= 6;
round(-1.1)= -1;

round - 语法

假设数字是“ x”。语法为:

无涯教程网

return_type round(data_type x);

round - 参数

x : 值可以是float或double。

round - 返回值

它返回x的舍入值。值的返回类型可以是float,double或long double。

round - 例子1

让我们看一个简单的例子,当x的值为正时

链接:https://www.learnfk.comhttps://www.learnfk.com/c++/cpp-math-round-function.html

来源:LearnFk无涯教程网

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    float x=8.3;  
    std::cout << "The value of x is : " <<x<< std::endl;  
    cout<<"Rounded value of x is : "<<round(x);  
    return 0;  
}  

输出:

The value of x is : 8.3
Rounded value of x is : 8   

round - 例子2

让我们看一个简单的例子,当x的值为负数时。

#include <iostream>  
#include<math.h>  
using namespace std;  
int main()  
{  
    double x=-9.9;  
    std::cout << "The value of x is : " <<x<< std::endl;  
    cout<<"Rounded value of x is : "<<round(x);  
    return 0;  
}  

输出:

The value of x is : -9.9
Rounded value of x is : -10

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学架构 -〔李运华〕

深入浅出计算机组成原理 -〔徐文浩〕

OpenResty从入门到实战 -〔温铭〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

Spark核心原理与实战 -〔王磊〕

分布式金融架构课 -〔任杰〕

代码之丑 -〔郑晔〕

手把手带你搭建秒杀系统 -〔佘志东〕

Midjourney入门实践课 -〔Jovi〕

好记忆不如烂笔头。留下您的足迹吧 :)