C ++ round () - Libreria standard C ++

La funzione round () in C ++ restituisce il valore integrale più vicino all'argomento, con i casi a metà arrotondati dallo zero.

La funzione round () in C ++ restituisce il valore integrale più vicino all'argomento, con i casi a metà arrotondati dallo zero.

prototipo round () (come dallo standard C ++ 11)

doppio giro (doppia x); float round (float x); lungo doppio giro (lungo doppio x); doppio giro (T x); // Per il tipo integrale

La funzione round () accetta un singolo argomento e restituisce un valore di tipo double, float o long double. Questa funzione è definita nel file di intestazione.

round () Parametri

La funzione round () richiede un singolo valore di argomento per arrotondare.

round () Restituisce il valore

La funzione round () restituisce il valore integrale più vicino a x, con i casi a metà arrotondati rispetto allo zero.

Esempio 1: come funziona round () in C ++?

 #include #include 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; )

Quando esegui il programma, l'output sarà:

 round (11,16) = 11 round (13,87) = 14 round (50,5) = 51 round (-11,16) = -11 round (-13,87) = -14 round (-50,5) = -51

Esempio 2: funzione round () per tipi integrali

 #include #include using namespace std; int main() ( int x = 15; double result; result = round(x); cout << "round(" << x << ") = " << result << endl; return 0; ) 

Quando esegui il programma, l'output sarà:

 rotondo (15) = 15 

Per i valori integrali, l'applicazione della funzione round restituisce lo stesso valore dell'input. Quindi non è comunemente usato per i valori integrali nella pratica.

Articoli interessanti...