NSTimeInterval 到可读 NSNumber

发布于 2024-07-13 23:25:59 字数 117 浏览 13 评论 0原文

NSTimeInterval == 双; (例如 169.12345666663)

如何舍入这个双精度数,以便“点”后只剩下 2 位数字?
如果结果是一个 NSNumber 那就太好了。

NSTimeInterval == double; (e.g. 169.12345666663)

How can I round up this double so that there are only 2 digits left after the "dot"?
It would be very good if the result is a NSNumber.

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

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

发布评论

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

评论(7

‖放下 2024-07-20 23:25:59

如果这是出于显示目的,请查看 NSNumberFormatter

如果出于某种原因您确实想在计算中对双精度进行舍入,则可以使用标准 C round() 函数。

If this is for display purposes, take a look at NSNumberFormatter.

If you really want to round the double in your calculations for some reason, you can use the standard C round() function.

如此安好 2024-07-20 23:25:59

NSDecimal 可以使用 NSDecimalRound()

double d = [[NSDate date] timeIntervalSince1970];
NSDecimal in = [[NSNumber numberWithDouble:d] decimalValue];
NSDecimal out;
NSDecimalRound( &out, &in, 2, NSRoundUp );

NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithDecimal:out];

A NSDecimal can be rounded to a specified number of digits with NSDecimalRound().

double d = [[NSDate date] timeIntervalSince1970];
NSDecimal in = [[NSNumber numberWithDouble:d] decimalValue];
NSDecimal out;
NSDecimalRound( &out, &in, 2, NSRoundUp );

NSDecimalNumber *result = [NSDecimalNumber decimalNumberWithDecimal:out];
百善笑为先 2024-07-20 23:25:59

如果您确实想要点后留下两位数字,请乘以 100,使用 round() 函数对其进行舍入,然后再次除以 100。 然而,这并不能保证它在点后确实只有两位数,因为通过再次除以它,您可能会得到一个无法真正用浮点表示法表达的数字,而当您期望 0.1 时,实际上可能会得到 0.09999 。 .,那是因为你无法真正使用浮点表示法来表达 0.1。

如果您只想将其四舍五入到点后两位数字以用于显示目的,您可以按照建议使用 NSNumberFormatter 或仅使用:

printf("%.2f\n", yourTimeInterval);
NSLog(@"%.2f\n", yourTimeInterval);

或者要获取 NSString,您还可以使用以下命令,这可能比使用更快NumberFormatter(但是,它不会根据用户首选项进行本地化):

NSString * intervalStr = nil;
char * intervalStrTmp = NULL;
asprintf(&intervalStrTmp, "%.2f", yourTimeInteval);
if (intervalStrTmp) {
  intervalStr = [[NSString alloc] initWithUTF8String:intervalStrTmp];
  free(intervalStrTmp);
}

If you really want two digits left after the dot, multiply by 100, round it using round() function, divide it by 100 again. However, this will not guarantee that it really has only two digits after the dot, since by dividing it again, you may get a number that cannot really be expressed with floating point notation and when you expect 0.1 you may in fact get 0.09999..., that's because you cannot really express 0.1 using floating point notation.

If you just want to round it to two digits after the dot for display purposes, you can use NSNumberFormatter as has been suggested or just use:

printf("%.2f\n", yourTimeInterval);
NSLog(@"%.2f\n", yourTimeInterval);

or to get an NSString, you can also use the following, which is probably even faster than using a NumberFormatter (however, it won't be localized according to the user prefs):

NSString * intervalStr = nil;
char * intervalStrTmp = NULL;
asprintf(&intervalStrTmp, "%.2f", yourTimeInteval);
if (intervalStrTmp) {
  intervalStr = [[NSString alloc] initWithUTF8String:intervalStrTmp];
  free(intervalStrTmp);
}
花伊自在美 2024-07-20 23:25:59

在绝大多数情况下,您应该只在显示时对数字进行四舍五入。 浮点数(双精度或非双精度)的属性使得不可能以固定精度存储某些数字。

有关格式化数字以使其显示为小数点后两位的信息,请参阅另一篇文章

In the vast majority of cases rounding a number is something you should only do at display time. The properties of floating-point numbers (double or not) make it impossible to store certain numbers at a fixed-precision.

For information about formatting a number so it displays to two decimal places, see this other post.

兮子 2024-07-20 23:25:59

这个 HumanReadableTimeInterval 有帮助吗? 但它返回一个 NSString。
或者,您可以通过乘以 100、转换为整数并再次除以 100 来进行四舍五入。

Does this HumanReadableTimeInterval help? It returns a NSString, though.
Alternatively, you can round yourself by multiplying with 100, converting to an integer and dividing through 100 again.

找回味觉 2024-07-20 23:25:59

我只使用 ANSI C round() 函数。

I would just use the ANSI C round() function.

梨涡 2024-07-20 23:25:59

您始终可以使用以下方法对数字进行舍入:

double round2dec(double a) { return round(a * 100) / 100; }

但结果的双精度数表示形式很可能不会只有 2 位小数。

现在,如果使用 == 符号,则意味着两个双精度数的比较仅与小数点后第二位进行。 您可以执行以下操作:

fabs(round2dec(NSTimeInterval) - round2dec(double)) < std::numeric_limits<double>::epsilon()

You can always round the number using:

double round2dec(double a) { return round(a * 100) / 100; }

But chances are that the representation of the result as a double will not have only 2 decimals.

Now, if by using the == sign, you meant that the comparison of your two double numbers is made only to the second decimal. Here is what you can do:

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