Freigeben über


round, roundf, roundl

Rundet einen Gleitkommawert auf den nächsten ganzzahligen Wert.

Syntax

double round(
   double x
);
float round(
   float x
);  // C++ only
long double round(
   long double x
);  // C++ only
float roundf(
   float x
);
long double roundl(
   long double x
);
#define round(X) // Requires C11 or higher

Parameter

x
Der zu rundende Gleitkommawert.

Rückgabewert

Die round-Funktionen geben einen Gleitkommawert zurück, der die nächste ganze Zahl zu x darstellt. Halbe Werte werden kaufmännisch gerundet, unabhängig von der Einstellung des Gleitkomma-Rundungsmodus. Es wird kein Fehler zurückgegeben.

Eingabe SEH-Ausnahme _matherr-Ausnahme
± QNaN, IND Keine _DOMAIN

Hinweise

Da C++ das Überladen zulässt, können Sie Überladungen von round aufrufen, die float - und long double -Werte verwenden und zurückgeben. In einem C-Programm, es sei denn, Sie verwenden das <tgmath.h> Makro, um diese Funktion aufzurufen, round verwendet immer und gibt eine doublezurück.

Wenn Sie das round Makro <tgmath.h>verwenden, bestimmt der Typ des Arguments, welche Version der Funktion ausgewählt ist. Ausführliche Informationen finden Sie unter "Typgenerika" .

Standardmäßig gilt der globale Zustand dieser Funktion für die Anwendung. Informationen zum Ändern dieses Verhaltens finden Sie im Global state in the CRT.

Anforderungen

Routine Erforderlicher Header
round, roundf, roundl <math.h>
round-Makro <tgmath.h>

Weitere Informationen zur Kompatibilität finden Sie unter Kompatibilität.

Beispiel

// Build with: cl /W3 /Tc
// This example displays the rounded
// results of floating-point values

#include <math.h>
#include <stdio.h>

int main()
{
    printf("===== Round a float\n\n");
    float floatValue = 2.4999999f; // float stores a value close to, but not exactly equal to, the initializer below. floatValue will contain 2.5 because it is the closest single precision value
    printf("roundf(%.1000g) is %.1000g\n", floatValue, roundf(floatValue));
    printf("roundf(%.1000g) is %.1000g\n", -floatValue, roundf(-floatValue));

    // double stores a value close to, but not exactly equal to, the initializer below. The closest double value is just slightly larger.
    double doubleValue = 2.4999999;
    printf("\n===== Round a double\n\n");
    printf("round(%.1000g) is %.1000g\n", doubleValue, round(doubleValue));
    printf("round(%.1000g) is %.1000g\n", -doubleValue, round(-doubleValue));

    // long double stores a value close to, but not exactly equal to, the initializer below. The closest long double value is just slightly larger.
    long double longDoubleValue = 2.4999999L;
    printf("\n===== Round a long double\n\n");
    printf("roundl(%.1000g) is %.1000g\n", longDoubleValue, roundl(longDoubleValue));
    printf("roundl(%.1000g) is %.1000g\n", -longDoubleValue, roundl(-longDoubleValue));

    return 0;
}
===== Round a float

roundf(2.5) is 3
roundf(-2.5) is -3

===== Round a double

round(2.499999900000000163657887242152355611324310302734375) is 2
round(-2.499999900000000163657887242152355611324310302734375) is -2

===== Round a long double

roundl(2.499999900000000163657887242152355611324310302734375) is 2
roundl(-2.499999900000000163657887242152355611324310302734375) is -2

Siehe auch

Mathematische und Gleitkommaunterstützung
ceil, ceilf, ceill
floor, floorf, floorl
fmod, fmodf
lrint, lrintf, lrintl, llrint, llrintf, llrintl
lround, lroundf, lroundl, llround, llroundf, llroundl
nearbyint, nearbyintf, nearbyintl
rint, rintf, rintl