繁体   English   中英

比较SQL Server中的钱与C#中的小数

[英]comparing money in sql server with decimal in C#

我在db中有一些类型为money的字段,而在C#中,我已经为它们使用了十进制。 我正在像这样比较C#代码中的值:

public static string CompareValues(DbPropertyValues currentValues, DbPropertyValues DbValues)
        {
            string result = string.Empty;

            foreach (var currentPropertyName in currentValues.PropertyNames)
            {
                foreach (var DBPropertyName in DbValues.PropertyNames)
                {
                    if (DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit" || DBPropertyName == "PropertyCreditLimit")


                    if ((currentPropertyName == DBPropertyName))
                    {
                        if (( currentValues[currentPropertyName] != null && DbValues != null) &&  (currentValues[currentPropertyName].ToString() != DbValues[DBPropertyName].ToString()))
                        {
                            result += string.Format("Property{0}, {1} =  {2}, {3} <br />", currentPropertyName, DbValues[DBPropertyName], currentValues[currentPropertyName], DBPropertyName);
                        }
                    }
                }
            }

            return result;
        }

但是由于零/精度的原因,它不匹配:

PropertyDateFirstReported, 3/8/2008 1:15:36 AM = 1/1/0001 12:00:00 AM, DateFirstReported 
PropertyCreditLimit, 564000.0000 = 564000.00, CreditLimit 
PropertyBalance, 45.0000 = 45.00, Balance 
PropertyMinimumInstallment, 0.0000 = 0.00, MinimumInstallment 
PropertyTerm, = Term1, Term 
PropertyPurpose, a = a1, Purpose 
PropertyCollateralValue, 0.0000 = 0.00, CollateralValue 
PropertyAccountUniqueID, 3767 = 0, AccountUniqueID 
PropertyPayment, 564000.0000 = 564000.00, Payment

如何解决呢?

 (currentValues[currentPropertyName].ToString() != DbValues[DBPropertyName].ToString()

当前,您将它们作为字符串进行比较这说明了为什么不存在匹配项-您必须直接比较这些值。

如果必须进行字符串比较,请使用“ f2”格式化程序来保证2个小数位。 否则,只需将DB值转换为(decimal)

大概是这样的:

bool isEqual = false;
if ( currentValues[currentPropertyName] != null && DbValues != null) {
 if (currentValues[currentPropertyName] is decimal && DbValues[DBPropertyName] is decimal) {
   isEqual = (Decimal)currentValues[currentPropertyName] == (Decimal)DbValues[DBPropertyName];
} else {
   isEqual = currentValues[currentPropertyName].ToString() == DbValues[DBPropertyName].ToString();
}

如果找到其他数据类型,而ToString()不能为您提供正确的比较值,则可以添加其他else子句。 或者,您可以添加它们以完全避免对ToString()调用。

如果要将它们作为字符串进行比较,则需要使用相同的格式,例如

.ToString(“ F02”,CultureInfo.InvariantCulture);

为什么不从数据库读取/写入时将Money转换为Decimal,您将无所不在。

尝试这个:

if (!object.Equals(currentValues[currentPropertyName], DbValues[DBPropertyName]))
{
  //Do stuff
}

备注:object.Equals(null,null)被视为true

不要转换为字符串以比较小数位数; 空值,填充,格式,本地化,有效数字,小数位都可以在运行时更改。 您正在代码中植入地雷。 做这个。

decimal? d1 = currentValues[currentPropertyName] as Nullable<decimal>;
decimal? d2 = (DbValues == null)? null : DbValues[DBPropertyName] as Nullable<decimal>;
if (d1 == d2)
    ...;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM