- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
round() in C++.
The round() function in C++ is used to round off the double, float or long double value passed to it as a parameter to the nearest integral value. The header file used to use the round() function in a c++ program is <cmath> or <tgmath>.
Following are the overloaded versions of round() after C++ 11 standard
- double round( double D )
- float round( float F )
- long double round( long double LD )
- double round ( T var )
Note − The value returned is the nearest integer represented as floating point, i.e for 2.3 nearest value returned will be 2.0 and not 2.
Following program is used to demonstrate the usage of round function in a C++ program −
Example
#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; }
Output
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
Advertisements