iPhone - 舍入浮动不起作用
我无法实现对浮点数的舍入。
这些调用都返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Objective-C 中的舍入数字:
这将为您提供一个舍入的字符串。我确信你可以将它解析回浮点数。
好的,这是一些测试代码和一些结果:
好的,你知道吗?您的原始代码在我的机器、OSX 10.6 和 Xcode 4 上按预期工作。您看到的输出到底如何?
From Rounding numbers in Objective-C:
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:
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?