2010-11-15 88 views
3

我想将可空的十进制转换为十进制的原始值。 如何做到这一点。 我做了一些Google搜索,发现System.ComponentModel.NullableConverter作为解决方案之一。但我无法弄清楚如何使用它。转换为小数?原始十进制

decimal? offenceAmount = 23; 
decimal primitive; 
primitive=offenceAmount; // 

请帮忙。

回答

6

你可以这样做:

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

或者,如果你想要的结果默认为0

primitive = offenceAmount.GetValueOrDefault(); 

或为上述的快捷方式:

primitive = offenseAmount ?? 0; 
1

试试这个。

primitive = (decimal)offenceAmount; 
3

您应该使用Nullable.Value财产:

if(offenceAmount.HasValue) 
    primitive = offenceAmount.Value; 
1
primitive = offenceAmount ?? 0;