iOS - rounding a float with roundf() is not working properly
问题 I am having issue with rounding a float in iPhone application. float f=4.845; float s= roundf(f * 100.0)/100; NSLog(@"Output-1: %.2f",s); s= roundf(484.5)/100; NSLog(@"Output-2: %.2f",s); Output-1: 4.84 Output-2: 4.85 Let me know whats problem in this and how to solve this. 回答1: The problem is that you don't yet realise one of the inherent problems with floating point: the fact that most numbers cannot be represented exactly (a) . This means that 4.845 is likely to be, in reality, something