C ++中的round()。
round()
C++中的函数用于将作为参数传递给它的double,float或longdouble值四舍五入到最接近的整数值。在c++程序中用于使用round()函数的头文件是<cmath>或<tgmath>。
以下是round()
C++11标准之后的重载版本
双轮(双D)
圆浮(F浮)
长双圆(长双LD)
双轮(Tvar)
注–返回的值是表示为浮点数的最接近的整数,即对于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