此函數用於四舍五入可以是浮點數或雙精度數的給定值。
例如:
round(5.8)= 6;
round(-1.1)= -1;
用法
假設一個數字是 'x'。語法是:
return_type round(data_type x);
參數
x:值可以是浮點數或雙精度數。
返回值
它返回 x 的舍入值。值的返回類型可以是 float、double 或 long double。
例子1
讓我們看一個簡單的例子,當 x 的值為正時
#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
例子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
相關用法
- C++ Math remquo()用法及代碼示例
- C++ Math remainder()用法及代碼示例
- C++ Math rint()用法及代碼示例
- C++ Math scalbn()用法及代碼示例
- C++ Math acosh()用法及代碼示例
- C++ Math asinh()用法及代碼示例
- C++ Math isgreater()用法及代碼示例
- C++ Math fabs()用法及代碼示例
- C++ Math islessgreater()用法及代碼示例
- C++ Math log2()用法及代碼示例
- C++ Math nearbyint()用法及代碼示例
- C++ Math tan()用法及代碼示例
- C++ Math log()用法及代碼示例
- C++ Math nextafter()用法及代碼示例
- C++ Math fdim()用法及代碼示例
- C++ Math isfinite()用法及代碼示例
- C++ Math erfc()用法及代碼示例
- C++ Math sinh()用法及代碼示例
- C++ Math scalbln()用法及代碼示例
- C++ Math cosh()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Math round()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。