English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
تقوم دالة round() في C++ بإرجاع القيمة الصحيحة الأقرب إلى المعامل، وتقوم بالتقريب من الصفر في الحالات المتوسطة.
double round(double x); float round(float x); long double round(long double x); double round(T x); // للنوع الصحيح
تستخدم دالة round() معامل واحد وتقوم بإرجاع قيمة من نوع double، float أو long double. تعمل هذه الدالة في<cmath>محددة في ملف الرأس.
تستخدم دالة round() قيمة معلمة واحدة للتقريب.
تقوم دالة round() بإرجاع القيمة الصحيحة الأقرب إلى x، وتقوم بالتقريب من الصفر في الحالات المتوسطة.
#include <iostream> #include <cmath> using namespace std; int main() { double x = 11.16, result; result = round(x); cout << "round(" << x << ") = " << result << endl; x = 13.87; result = round(x); cout << "round(" << x << ") = " << result << endl; x = 50.5; result = round(x); cout << "round(" << x << ") = " << result << endl; x = -11.16; result = round(x); cout << "round(" << x << ") = " << result << endl; x = -13.87; result = round(x); cout << "round(" << x << ") = " << result << endl; x = -50.5; result = round(x); cout << "round(" << x << ") = " << result << endl; return 0; }
عند تشغيل البرنامج، الناتج هو:
round(11.16) = 11 round(13.87) = 14 round(50.5) = 51 round(-11.16) = -11 round(-13.87) = -14 round(-50.5) = -51
#include <iostream> #include <cmath> using namespace std; int main() { int x = 15; double result; result = round(x); cout << "round(" << x << ") = " << result << endl; return 0; }
عند تشغيل البرنامج، الناتج هو:
round(15) = 15
بالنسبة للقيم الصحيحة، ستعود وظيفة round بنفس القيمة المدخلة. لذلك، لا تستخدم عادة لتعيين القيم الصحيحة.