Teilen über


round, roundfroundl

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 gibt keine Fehlerrückgabe.

Eingabe SEH-Ausnahme _matherr-Ausnahme
± QNaN, IND none _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 Typgengenerische Mathematik.

Standardmäßig gilt der globale Zustand dieser Funktion für die Anwendung. Wie Sie dieses Verhalten ändern, erfahren Sie unter Globaler Status in der CRT.

Anforderungen

Routine Erforderlicher Header
round, roundfroundl <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 Unterstützung und Gleitkommaunterstützung
ceil, ceilfceill
floor, floorffloorl
fmod, fmodf
lrint, , lrintflrintl, llrint, , llrintfllrintl
lround, , lroundflroundl, llround, , llroundfllroundl
nearbyint, nearbyintfnearbyintl
rint, rintfrintl