检查十进制值是否为空

时间:2022-01-28 17:07:00

I would like to check if the decimal number is NULL or it has some value, since the value is assigned from database in class object:

我想检查一下这个十进制数是NULL还是有一些值,因为这个值是从class object的数据库中赋值的:

public decimal myDecimal{ get; set; }

and then I have

然后我有

myDecimal = Convert.ToDecimal(rdrSelect[23].ToString());

I am trying:

我尝试:

if (rdrSelect[23] != DBNull.Value)
                    {
                        myDecimal = Convert.ToDecimal(rdrSelect[23].ToString());
                    }

But I am getting this:

但我得到的是:

the result of the expression is always 'true' since a value of type 'decimal' is never equal to null

表达式的结果总是'true',因为'decimal'类型的值从不等于null

How can I check if that decimal number has some value?

我如何检查那个小数是否有值?

7 个解决方案

#1


34  

A decimal will always have some default value. If you need to have a nullable type decimal, you can use decimal?. Then you can do myDecimal.HasValue

小数总是有一些默认值。如果需要一个可空类型decimal,可以使用decimal吗?然后可以做mydecimal。hasvalue

#2


3  

decimal is a value type in .NET. And value types can't be null. But if you use nullable type for your decimal, then you can check your decimal is null or not. Like myDecimal?

decimal是. net中的值类型。值类型不能为空。但是如果你对你的小数使用可空类型,那么你可以检查你的小数是否为空。喜欢myDecimal吗?

Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

可空类型是系统的实例。Nullable结构。可空类型可以表示其基础值类型的正常值范围,外加一个额外的空值。

if (myDecimal.HasValue)

But I think in your database, if this column contains nullable values, then it shouldn't be type of decimal.

但是我认为在你的数据库中,如果这个列包含可空值,那么它不应该是十进制的类型。

#3


3  

you can use this code

您可以使用此代码

if (DecimalVariable.Equals(null))  
{
   //something statements
}

#4


1  

Assuming you are reading from a data row, what you want is:

假设您正在读取数据行,那么您需要的是:

if ( !rdrSelect.IsNull(23) ) 
{ 
   //handle parsing
}

#5


1  

Decimal is a value type, so if you wish to check whether it has a value other than the value it was initialised with (zero) you can use the condition myDecimal != default(decimal).

Decimal是一种值类型,因此,如果您希望检查它是否具有与用(0)初始化的值不同的值,您可以使用条件myDecimal != default(Decimal)。

Otherwise you should possibly consider the use of a nullable (decimal?) type and the use a condition such as myNullableDecimal.HasValue

否则,您应该考虑使用可空(decimal?)类型,并使用条件,如myNullableDecimal.HasValue

#6


0  

If you're pulling this value directly from a SQL Database and the value is null in there, it will actually be the DBNull object rather than null. Either place a check prior to your conversion & use a default value in the event of DBNull, or replace your null check afterwards with a check on rdrSelect[23] for DBNull.

如果您直接从SQL数据库中提取这个值,并且其中的值为null,那么它实际上是DBNull对象,而不是null。要么在转换之前进行检查,并在DBNull事件中使用默认值,要么在之后用rdrSelect[23]检查DBNull来替换空检查。

#7


0  

You can also create a handy utility functions to handle values from DB in cases like these. Ex. Below is the function which gives you Nullable Decimal from object type.

您还可以创建一个方便的实用函数来处理像这样的情况下来自DB的值。下面是一个函数,它从对象类型中给出可为零的小数。

    public static decimal? ToNullableDecimal(object val)
    {
        if (val is DBNull ||
            val == null)
        {
            return null;
        }
        if (val is string &&
            ((string)val).Length == 0)
        {
            return null;
        }
        return Convert.ToDecimal(val);
    } 

#1


34  

A decimal will always have some default value. If you need to have a nullable type decimal, you can use decimal?. Then you can do myDecimal.HasValue

小数总是有一些默认值。如果需要一个可空类型decimal,可以使用decimal吗?然后可以做mydecimal。hasvalue

#2


3  

decimal is a value type in .NET. And value types can't be null. But if you use nullable type for your decimal, then you can check your decimal is null or not. Like myDecimal?

decimal是. net中的值类型。值类型不能为空。但是如果你对你的小数使用可空类型,那么你可以检查你的小数是否为空。喜欢myDecimal吗?

Nullable types are instances of the System.Nullable struct. A nullable type can represent the normal range of values for its underlying value type, plus an additional null value.

可空类型是系统的实例。Nullable结构。可空类型可以表示其基础值类型的正常值范围,外加一个额外的空值。

if (myDecimal.HasValue)

But I think in your database, if this column contains nullable values, then it shouldn't be type of decimal.

但是我认为在你的数据库中,如果这个列包含可空值,那么它不应该是十进制的类型。

#3


3  

you can use this code

您可以使用此代码

if (DecimalVariable.Equals(null))  
{
   //something statements
}

#4


1  

Assuming you are reading from a data row, what you want is:

假设您正在读取数据行,那么您需要的是:

if ( !rdrSelect.IsNull(23) ) 
{ 
   //handle parsing
}

#5


1  

Decimal is a value type, so if you wish to check whether it has a value other than the value it was initialised with (zero) you can use the condition myDecimal != default(decimal).

Decimal是一种值类型,因此,如果您希望检查它是否具有与用(0)初始化的值不同的值,您可以使用条件myDecimal != default(Decimal)。

Otherwise you should possibly consider the use of a nullable (decimal?) type and the use a condition such as myNullableDecimal.HasValue

否则,您应该考虑使用可空(decimal?)类型,并使用条件,如myNullableDecimal.HasValue

#6


0  

If you're pulling this value directly from a SQL Database and the value is null in there, it will actually be the DBNull object rather than null. Either place a check prior to your conversion & use a default value in the event of DBNull, or replace your null check afterwards with a check on rdrSelect[23] for DBNull.

如果您直接从SQL数据库中提取这个值,并且其中的值为null,那么它实际上是DBNull对象,而不是null。要么在转换之前进行检查,并在DBNull事件中使用默认值,要么在之后用rdrSelect[23]检查DBNull来替换空检查。

#7


0  

You can also create a handy utility functions to handle values from DB in cases like these. Ex. Below is the function which gives you Nullable Decimal from object type.

您还可以创建一个方便的实用函数来处理像这样的情况下来自DB的值。下面是一个函数,它从对象类型中给出可为零的小数。

    public static decimal? ToNullableDecimal(object val)
    {
        if (val is DBNull ||
            val == null)
        {
            return null;
        }
        if (val is string &&
            ((string)val).Length == 0)
        {
            return null;
        }
        return Convert.ToDecimal(val);
    }