How to properly compare decimal values in C#?

后端 未结 4 740
忘掉有多难
忘掉有多难 2020-12-16 09:23

I come from a background in C++, and I know that you cannot accurately compare floats for equality. For C#, I simply assumed the same policy applies to decimal values, or an

4条回答
  •  眼角桃花
    2020-12-16 09:40

    I was investigating something similar, but with a precision instead of a margin of error and ended up writing some extensions for Float. This can easily be adapted for any type though. I've got a complicated series of comparisons and this makes it nice and readable.

    /// 
    /// A set of extensions to allow the convenient comparison of float values based on a given precision.
    /// 
    public static class FloatingPointExtensions
    {
        /// 
        /// Determines if the float value is less than or equal to the float parameter according to the defined precision.
        /// 
        /// The float1.
        /// The float2.
        /// The precision.  The number of digits after the decimal that will be considered when comparing.
        /// 
        public static bool LessThan(this float float1, float float2, int precision)
        {
            return (System.Math.Round(float1 - float2, precision) < 0);
        }
    
        /// 
        /// Determines if the float value is less than or equal to the float parameter according to the defined precision.
        /// 
        /// The float1.
        /// The float2.
        /// The precision.  The number of digits after the decimal that will be considered when comparing.
        /// 
        public static bool LessThanOrEqualTo(this float float1, float float2, int precision)
        {
            return (System.Math.Round(float1 - float2, precision) <= 0);
        }
    
        /// 
        /// Determines if the float value is greater than (>) the float parameter according to the defined precision.
        /// 
        /// The float1.
        /// The float2.
        /// The precision.  The number of digits after the decimal that will be considered when comparing.
        /// 
        public static bool GreaterThan(this float float1, float float2, int precision)
        {
            return (System.Math.Round(float1 - float2, precision) > 0);
        }
    
        /// 
        /// Determines if the float value is greater than or equal to (>=) the float parameter according to the defined precision.
        /// 
        /// The float1.
        /// The float2.
        /// The precision.  The number of digits after the decimal that will be considered when comparing.
        /// 
        public static bool GreaterThanOrEqualTo(this float float1, float float2, int precision)
        {
            return (System.Math.Round(float1 - float2, precision) >= 0);
        }
    
        /// 
        /// Determines if the float value is equal to (==) the float parameter according to the defined precision.
        /// 
        /// The float1.
        /// The float2.
        /// The precision.  The number of digits after the decimal that will be considered when comparing.
        /// 
        public static bool AlmostEquals(this float float1, float float2, int precision)
        {
            return (System.Math.Round(float1 - float2, precision) == 0);
        } 
    }
    

提交回复
热议问题