将 Objective-C 浮点数四舍五入到最接近的 0.05

发布于 2024-11-02 06:18:19 字数 152 浏览 0 评论 0原文

我想将以下浮点数四舍五入到最接近的 0.05。

449.263824 --> 449.25

390.928070 --> 390.90

390.878082 --> 390.85

我怎样才能做到这一点?

I want to round the following floating point numbers to the nearest 0.05.

449.263824 --> 449.25

390.928070 --> 390.90

390.878082 --> 390.85

How can I accomplish that?

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

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

发布评论

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

评论(6

别念他 2024-11-09 06:18:19

为了匹配您问题中的输出,您可以执行以下操作:

float customRounding(float value) {
    const float roundingValue = 0.05;
    int mulitpler = floor(value / roundingValue);
    return mulitpler * roundingValue;
}

示例:

NSLog(@"Output: %f --> %.2f", 449.263824, customRounding(449.263824));

The match the output in your question, you can do the following:

float customRounding(float value) {
    const float roundingValue = 0.05;
    int mulitpler = floor(value / roundingValue);
    return mulitpler * roundingValue;
}

Example:

NSLog(@"Output: %f --> %.2f", 449.263824, customRounding(449.263824));
谈场末日恋爱 2024-11-09 06:18:19

round() 功能。我认为你需要这样做:

double rounded = round(number * 20.0) / 20.0;

与所有浮点运算一样,由于 1/5 不能直接表示为二进制值,因此你会看到奇怪的不完全精确的结果。如果你不喜欢这样,你可以使用 NSDecimalNumber 的 -decimalNumberByRoundingAccordingToBehaviour: 方法,但会慢一点。

There's the round() function. I think you need to do this:

double rounded = round(number * 20.0) / 20.0;

As with all floating point operations, since 1/5 is not directly representable as a binary value, you'll see bizarre not quite exact results. If you don't like that, you can use NSDecimalNumber's -decimalNumberByRoundingAccordingToBehaviour: method but it'll be a bit slower.

情魔剑神 2024-11-09 06:18:19

我知道问题已得到解答,但我使用了以下代码:

float unrounded = 2.234;

float decimal = 0.05;
float decimal2 = 1/decimal;
float rounded = (((int)((unrounded*decimal2)+0.5))/decimal2);

例如:

> unrounded = 2.234 
> decimal = 0.05
> decimal2 = 1/0.05 = 20
> 
> rounded:
> 2.234 * 20 = 44.68
> 44.68 + 0.5 = 45.18 
> make an integer: 45 
> 45 / 20 = 2.25

I know the question is answered but I used the following code:

float unrounded = 2.234;

float decimal = 0.05;
float decimal2 = 1/decimal;
float rounded = (((int)((unrounded*decimal2)+0.5))/decimal2);

For example:

> unrounded = 2.234 
> decimal = 0.05
> decimal2 = 1/0.05 = 20
> 
> rounded:
> 2.234 * 20 = 44.68
> 44.68 + 0.5 = 45.18 
> make an integer: 45 
> 45 / 20 = 2.25
二智少女 2024-11-09 06:18:19

您可以使用 NSNumberFormatter< /a> 进行舍入,并通过 NSNumberFormatterRoundingMode 选项之一指定所需的舍入。 (在上面的类参考中搜索“NSNumberFormatterRoundingMode”以查看默认值。)

但是,正如 @Jesse 在对您的问题的评论中指出的那样,在您的示例中似乎没有任何标准的舍入形式假如。

You could use an NSNumberFormatter to carry out rounding and indeed to specify the rounding you require via one of the NSNumberFormatterRoundingMode options. (Search for "NSNumberFormatterRoundingMode" in the above class reference to see the defaults.)

However, as @Jesse states in the comment on your question, there doesn't seems to be any standard form of rounding going on in the examples you're provided.

蓝颜夕 2024-11-09 06:18:19

如果它四舍五入到最接近的 x,那么您可以选择:

roundedValue = originalValue + x * 0.5;
roundedValue -= fmodf(roundedValue, x);

事实上,并不完全清楚您想要什么。

If it were round to the nearest x, then you could go with:

roundedValue = originalValue + x * 0.5;
roundedValue -= fmodf(roundedValue, x);

As it is, it isn't entirely clear what you want.

折戟 2024-11-09 06:18:19

使用地板

#include <math.h>
...
double result = floor(number * 20.0) / 20.0;

Use floor:

#include <math.h>
...
double result = floor(number * 20.0) / 20.0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文