C++ 中的 round()。
C++ 中的 round() 函式用於將傳遞給它作為引數的 double、float 或 long double 值四捨五入到最近的整數。用於在 c++ 程式中使用 round() 函式的標頭檔案是 <cmath> 或 <tgmath>。
以下是 C++ 11 標準之後的 round() 的過載版本
- double round( double D )
- float round( float F )
- long double round( long double LD )
- double round ( T var )
注意 - 返回的值是最近的整數,表示為浮點數,即對於 2.3,返回的最近值將是 2.0 而不是 2。
以下程式用於演示在 C++ 程式中使用 round 函式 -
示例
#include <cmath>
#include <iostream>
int main(){
double num1=10.5;
double num2=10.3;
double num3=9.7;
std::cout << "Nearest integer after round("<<num1<<") :" << round(num1)<< "\n";
std::cout << "Nearest integer after round("<<num2<<") :" << round(num2)<< "\n";
std::cout << "Nearest integer after round("<<num3<<") :" << round(num3)<< "\n";
num1=-9.3;
num2=-0.3;
num3=-9.9;
std::cout << "Nearest integer after round("<<num1<<") :" << round(num1)<< "\n";
std::cout << "Nearest integer after round("<<num2<<") :" << round(num2)<< "\n";
std::cout << "Nearest integer after round("<<num3<<") :" << round(num3)<< "\n";
return 0;
}輸出
Nearest integer after round(10.5) :11 Nearest integer after round(10.3) :10 Nearest integer after round(9.7) :10 Nearest integer after round(-9.3) :-9 Nearest integer after round(-0.3) :-0 Nearest integer after round(-9.9) :-10
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP