我无法实现舍入浮动。iPhone - 舍入浮动不起作用
那些调用返回给我3.5999999,而不是3.6 foroTimeoutTrick和theoTimeout。
我怎样才能将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;
3.6不能精确地用二进制表示,并且有很多数字。 –
@ Kerrek SB:我可以理解,但我不在乎。我必须显示3.6,这是正确的四舍五入值。例如,你有没有在你的银行账户上看过这样的小数? – Oliver
我敢肯定我的银行账户不会使用IEEE浮点数作为余额! (主要是因为他们的范围有限:-))。使用格式化输出来打印适当的四舍五入值。 –