Share via


lround, lroundf, lroundl, llround, llroundf, llroundl

將浮點值四捨五入為最接近的整數。

語法

long lround(
   double x
);
long lround(
   float x
);  // C++ only
long lround(
   long double x
);  // C++ only
long lroundf(
   float x
);
long lroundl(
   long double x
);
long long llround(
   double x
);
long long llround(
   float x
);  // C++ only
long long llround(
   long double x
);  // C++ only
long long llroundf(
   float x
);
long long llroundl(
   long double x
);
#define lround(X) // Requires C11 or higher

參數

x
要四捨五入的浮點值。

傳回值

lroundllround 函式會將最接近的 longlong long 整數傳回 x。 不論浮點進位模式設定為何,中間值都會背離零四捨五入。 沒有傳回錯誤。

輸入 SEH 例外狀況 _matherr 例外
± QNaN,IND none _DOMAIN

備註

因為 C++ 允許多載,因此您可以呼叫 lroundllround 多載,接受 並傳回 floatlong double 值。 在 C 程式中,除非您使用 < tgmath.h > 宏來呼叫此函式, lround 而且 llround 一律接受並傳回 double

如果您使用 < tgmath.h >lround() 宏,引數的類型會決定選取哪一個函式版本。 如需詳細資訊,請參閱 類型泛型數學

根據預設,此函式的全域狀態會限定于應用程式。 若要變更此行為,請參閱 CRT 中的全域狀態。

需求

常式 必要的標頭
lround, lroundf, lroundl, llround, llroundf, llroundl <math.h>
lround 宏觀 <tgmath.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_lround.c
// Build with: cl /W4 /Tc crt_lround.c
// This example displays the rounded results of
// the floating-point values 2.499999, -2.499999,
// 2.8, -2.8, 3.5 and -3.5.

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

int main( void )
{
   double x = 2.499999;
   float y = 2.8f;
   long double z = 3.5L;

   printf("lround(%f) is %d\n", x, lround(x));
   printf("lround(%f) is %d\n", -x, lround(-x));
   printf("lroundf(%f) is %d\n", y, lroundf(y));
   printf("lroundf(%f) is %d\n", -y, lroundf(-y));
   printf("lroundl(%Lf) is %d\n", z, lroundl(z));
   printf("lroundl(%Lf) is %d\n", -z, lroundl(-z));
}
lround(2.499999) is 2
lround(-2.499999) is -2
lroundf(2.800000) is 3
lroundf(-2.800000) is -3
lroundl(3.500000) is 4
lroundl(-3.500000) is -4

另請參閱

數學和浮點支援
ceil, ceilf, ceill
floor, floorf, floorl
fmod, fmodf
lrint, lrintf, lrintl, llrint, llrintf, llrintl
round, roundf, roundl
nearbyint, nearbyintf, nearbyintl
rint, rintf, rintl