This question already has answers here:
My floating point number has extra digits when I print it
                                
                                    (2个答案)
                                
                        
                        
                            Extra precision values seen for float values
                                
                                    (4个答案)
                                
                        
                                5年前关闭。
            
                    
如果我创建一个NSNumber且其值为0.085的双精度值,则似乎在数字上增加了额外的精度。但是,0.85并非如此。我不明白为什么会这样,但是我敢肯定有一个很明显的答案。有人知道为什么吗?

例如:

NSNumber *n1 = [NSNumber numberWithDouble:0.85];
NSNumber *n2 = [NSNumber numberWithDouble:0.085];

NSLog(@"Number: %@", n1);     // Number: 0.85
NSLog(@"Number: %@", n2);     // Number: 0.08500000000000001

最佳答案

这与double而不是NSNumber有关。 double是一个floating-point number值,根据设计,它不提供特定级别的精确小数点位数。

关于ios - 浮点数不精确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24676327/

10-12 19:02