如何在适用于 iPhone 的 iOS4 的 xcode 中将浮点数舍入为整数?

发布于 2024-12-09 13:02:21 字数 246 浏览 2 评论 0原文

可能的重复:
Objective-c:如何舍入浮动值? < /p>

我知道它与 NSNumberformatter 类有关,请问有示例代码吗?

Possible Duplicate:
Objective-c: How to round off Float values?

I know its with the NSNumberformatter class, any sample code please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

祁梦 2024-12-16 13:02:21

由于 int 无法存储浮点数,因此浮点数的小数部分将被截断,您将得到整个数字:

float x = 3.14159;
int y = (int)x;

这是有效的,因为 int只能存储整数,因此它只存储 x 的整数部分


另请参阅 gnu C 库舍入功能

 — Function: double ceil (double x)
 — Function: float ceilf (float x)
 — Function: long double ceill (long double x)
 — Function: double floor (double x)
 — Function: float floorf (float x)
 — Function: long double floorl (long double x)
 — Function: double trunc (double x)
 — Function: float truncf (float x)
 — Function: long double truncl (long double x)
 — Function: double rint (double x)
 — Function: float rintf (float x)
 — Function: long double rintl (long double x)
 — Function: double nearbyint (double x)
 — Function: float nearbyintf (float x)
 — Function: long double nearbyintl (long double x)
 — Function: double round (double x)
 — Function: float roundf (float x)
 — Function: long double roundl (long double x)
 — Function: long int lrint (double x)
 — Function: long int lrintf (float x)
 — Function: long int lrintl (long double x)
 — Function: long long int llrint (double x)
 — Function: long long int llrintf (float x)
 — Function: long long int llrintl (long double x)
 — Function: long int lround (double x)
 — Function: long int lroundf (float x)
 — Function: long int lroundl (long double x)
 — Function: long long int llround (double x)
 — Function: long long int llroundf (float x)
 — Function: long long int llroundl (long double x)
 — Function: double modf (double value, double *integer-part)
 — Function: float modff (float value, float *integer-part)
 — Function: long double modfl (long double value, long double *integer-part)

Since ints can't store floating-point numbers, the decimal portion of a float will be truncated and you will be left with the whole number:

float x = 3.14159;
int y = (int)x;

This works because the int is only capable of storing integers, so it simply stores the integer portion of x


See also gnu C Library Rounding Functions:

 — Function: double ceil (double x)
 — Function: float ceilf (float x)
 — Function: long double ceill (long double x)
 — Function: double floor (double x)
 — Function: float floorf (float x)
 — Function: long double floorl (long double x)
 — Function: double trunc (double x)
 — Function: float truncf (float x)
 — Function: long double truncl (long double x)
 — Function: double rint (double x)
 — Function: float rintf (float x)
 — Function: long double rintl (long double x)
 — Function: double nearbyint (double x)
 — Function: float nearbyintf (float x)
 — Function: long double nearbyintl (long double x)
 — Function: double round (double x)
 — Function: float roundf (float x)
 — Function: long double roundl (long double x)
 — Function: long int lrint (double x)
 — Function: long int lrintf (float x)
 — Function: long int lrintl (long double x)
 — Function: long long int llrint (double x)
 — Function: long long int llrintf (float x)
 — Function: long long int llrintl (long double x)
 — Function: long int lround (double x)
 — Function: long int lroundf (float x)
 — Function: long int lroundl (long double x)
 — Function: long long int llround (double x)
 — Function: long long int llroundf (float x)
 — Function: long long int llroundl (long double x)
 — Function: double modf (double value, double *integer-part)
 — Function: float modff (float value, float *integer-part)
 — Function: long double modfl (long double value, long double *integer-part)
鹿童谣 2024-12-16 13:02:21

但如果将其转换为 int,3.999 也等于 3。

float value = 3.14159f;
int intValue = (int)value;
float fractional = fmodf(value, (float)intValue);
if(fractional > .5f)
    intValue++;

intValue 是您的舍入整数。

But 3.999 is also equal to 3 if you just cast it to int.

float value = 3.14159f;
int intValue = (int)value;
float fractional = fmodf(value, (float)intValue);
if(fractional > .5f)
    intValue++;

intValue is your rounded int.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文