round, roundf, roundl

Membulatkan nilai titik mengambang ke nilai bilangan bulat terdekat.

Sintaks

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
Nilai floating-point untuk dibulatkan.

Nilai hasil

Fungsi round mengembalikan nilai floating-point yang mewakili bilangan bulat terdekat ke x. Nilai setengah dibulatkan jauh dari nol, terlepas dari pengaturan mode pembulatan floating-point. Tidak ada pengembalian kesalahan.

Input Pengecualian SEH Pengecualian _matherr
± QNaN, IND tidak ada _DOMAIN

Keterangan

Karena C++ memungkinkan kelebihan beban, Anda dapat memanggil kelebihan beban round yang mengambil dan mengembalikan float dan long double nilai. Dalam program C, kecuali Anda menggunakan <tgmath.h> makro untuk memanggil fungsi ini, round selalu mengambil dan mengembalikan double.

Jika Anda menggunakan round makro dari <tgmath.h>, jenis argumen menentukan versi fungsi mana yang dipilih. Lihat Matematika generik jenis untuk detailnya.

Secara default, status global fungsi ini dicakup ke aplikasi. Untuk mengubah perilaku ini, lihat Status global di CRT.

Persyaratan

Rutin Header yang diperlukan
round, roundf, roundl <math.h>
round makro <tgmath.h>

Untuk informasi kompatibilitas selengkapnya, lihat Kompatibilitas.

Contoh

// 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

Baca juga

Dukungan matematika dan titik mengambang
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