This question already has answers here:
Why am I getting the wrong result when using float? [duplicate]
                                
                                    (4个答案)
                                
                        
                        
                            Float is converting my values
                                
                                    (4个答案)
                                
                        
                                6年前关闭。
            
                    
结果必须是806603.77,但是为什么我得到806603.8?

float a = 855000.00f;
float b = 48396.23f;

float res = a - b;
Console.WriteLine(res);
Console.ReadKey();

最佳答案

您应该改用decimal,因为float仅具有7位精度的32-bit,这就是结果不同的原因,另一方面,decimal具有28-29位精度的128-bit

decimal a = 855000.00M;
decimal b = 48396.23M;

decimal res = a - b;
Console.WriteLine(res);
Console.ReadKey();


输出:806603.77

10-07 19:21