C# 检查十进制值是否为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14591057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if decimal value is null
提问by Laziale
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 或它有一些值,因为该值是从类对象中的数据库分配的:
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
表达式的结果始终为“真”,因为“十进制”类型的值永远不等于 null
How can I check if that decimal number has some value?
如何检查该十进制数是否具有某些值?
采纳答案by Justin
A decimal will always have some default value. If you need to have a nullable typedecimal, you can use decimal?
. Then you can do myDecimal.HasValue
小数总是有一些默认值。如果你需要一个可为空类型的十进制,你可以使用decimal?
. 然后你可以做myDecimal.HasValue
回答by Soner G?nül
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
是value type
.NET 中的一个。并且值类型不能是null
. 但是如果你使用nullable type
for your decimal
,那么你可以检查你的decimal
isnull
或 not 。喜欢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 nullvalue.
可空类型是 System.Nullable 结构的实例。可空类型可以表示其基础值类型的正常值范围,外加一个额外的空值。
if (myDecimal.HasValue)
But I think in your database, if this column contains nullable values, then it shouldn'tbe type of decimal
.
但我认为在您的数据库中,如果此列包含可为空的值,那么它不应该是decimal
.
回答by Chris
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]
for DBNull
。
回答by DotNetUser
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);
}
回答by Osa E
Assuming you are reading from a data row, what you want is:
假设您正在读取数据行,您想要的是:
if ( !rdrSelect.IsNull(23) )
{
//handle parsing
}
回答by reza akhlaghi
you can use this code
你可以使用这个代码
if (DecimalVariable.Equals(null))
{
//something statements
}
回答by SpaceKat
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 是一种值类型,所以如果你想检查它是否有一个不同于它初始化的值(零)的值,你可以使用条件 myDecimal != default(decimal)。
Otherwise you should possibly consider the use of a nullable (decimal?) type and the use a condition such as myNullableDecimal.HasValue
否则,您可能应该考虑使用可为空(十进制?)类型并使用诸如 myNullableDecimal.HasValue 之类的条件
回答by OrionMD
I've ran across this problem recently while trying to retrieve a null decimal from a DataTable
object from db and I haven't seen this answer here. I find this easier and shorter:
我最近在尝试DataTable
从 db的对象中检索空小数时遇到了这个问题,但我在这里没有看到这个答案。我发现这更容易和更短:
var value = rdrSelect.Field<decimal?>("ColumnName") ?? 0;
This was useful in my case since i didn't have a nullable decimal in the model, but needed a quick check against one. If the db value happens to be null, it'll just assign the default value.
这对我来说很有用,因为我在模型中没有可以为空的小数,但需要快速检查一个。如果 db 值恰好为空,它只会分配默认值。