iPhone - 舍入浮动不起作用

发布于 2024-11-26 01:21:32 字数 711 浏览 2 评论 0原文

我无法实现对浮点数的舍入。

这些调用都返回 3.5999999,而不是 theoTimeoutTrick 和 theoTimeout 的 3.6。
我如何才能将 3.6 值放入 NSString 和 float vars 中?

#define minTimeout 1.0
#define defaultNbRetry 5

    float secondsWaitedForAnswer = 20.0;
    float minDelayBetween2Retry = 0.5;

    int theoNbRetries = defaultNbRetry;
    float theoTimeout = 0.0;

    while (theoTimeout < minTimeout && theoNbRetries > 0) {
        theoTimeout = (secondsWaitedForAnswer - (theoNbRetries-1)*minDelayBetween2Retry) / theoNbRetries;
        theoNbRetries--;
    }

    float theoTimeoutTrick = [[NSString stringWithFormat:@"%.1f", theoTimeout] floatValue];
    theoTimeout = roundf(theoTimeout * 10)/10.0;

I can't achieve rounding a float.

Those calls all returns me 3.5999999 and not 3.6 for theoTimeoutTrick and theoTimeout.
How may I achieve to get that 3.6 value, into NSString AND float vars ?

#define minTimeout 1.0
#define defaultNbRetry 5

    float secondsWaitedForAnswer = 20.0;
    float minDelayBetween2Retry = 0.5;

    int theoNbRetries = defaultNbRetry;
    float theoTimeout = 0.0;

    while (theoTimeout < minTimeout && theoNbRetries > 0) {
        theoTimeout = (secondsWaitedForAnswer - (theoNbRetries-1)*minDelayBetween2Retry) / theoNbRetries;
        theoNbRetries--;
    }

    float theoTimeoutTrick = [[NSString stringWithFormat:@"%.1f", theoTimeout] floatValue];
    theoTimeout = roundf(theoTimeout * 10)/10.0;

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

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

发布评论

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

评论(1

↘人皮目录ツ 2024-12-03 01:21:32

来自 Objective-C 中的舍入数字

float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];

这将为您提供一个舍入的字符串。我确信你可以将它解析回浮点数。

好的,这是一些测试代码和一些结果:

NSLog(@"%f", round(10*3.56)/10.0);
    =>3.600000
NSLog(@"%f", round(10*3.54)/10.0);
    =>3.500000
NSLog(@"%f", round(10*3.14)/10.0);
    =>3.100000

好的,你知道吗?您的原始代码在我的机器、OSX 10.6 和 Xcode 4 上按预期工作。您看到的输出到底如何?

From Rounding numbers in Objective-C:

float roundedValue = round(2.0f * number) / 2.0f;
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:1];
[formatter setRoundingMode: NSNumberFormatterRoundDown];

NSString *numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:roundedValue]];
[formatter release];

That will get you a rounded string. You can parse it back into a float I'm sure.

Okay, here's some test code and some results:

NSLog(@"%f", round(10*3.56)/10.0);
    =>3.600000
NSLog(@"%f", round(10*3.54)/10.0);
    =>3.500000
NSLog(@"%f", round(10*3.14)/10.0);
    =>3.100000

Okay, you know what? Your original code works as intended on my machine, OSX 10.6 and Xcode 4. How exactly are you seeing your output?

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