将原始十进制数转换为十进制数?

3
我想将可空的十进制数转换为基本的十进制数。 如何操作呢? 我做了一些谷歌搜索,发现System.ComponentModel.NullableConverter是其中一个解决方案。但我不知道如何使用它。
decimal? offenceAmount = 23;
decimal primitive;
primitive=offenceAmount; //

Please help.

4个回答

6

您可以做以下事情:

if (offenceAmount.HasValue) {
    primitive = offenceAmount.Value;
}

或者,如果您希望结果默认为 0

primitive = offenceAmount.GetValueOrDefault();

或者可以使用上述内容的快捷方式:

primitive = offenseAmount ?? 0;

3

你应该使用 Nullable.Value 属性:

if(offenceAmount.HasValue)
    primitive = offenceAmount.Value;

1
 primitive = offenceAmount ?? 0;

1

试一下。

primitive = (decimal)offenceAmount;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接