Convert decimal numbers? in primitive decimal

I want to convert a decimal number with a zero value to a primitive decimal. How to do it. I did some searches and found System.ComponentModel.NullableConverter as one of the solutions. But I can’t figure out how to use it.

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

Please, help.

+3
source share
4 answers

You can do:

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

Or, if you want the result to be equal by default 0:

primitive = offenceAmount.GetValueOrDefault();

Or a shortcut for the above:

primitive = offenseAmount ?? 0;
+6
source

You should use the property Nullable.Value:

if(offenceAmount.HasValue)
    primitive = offenceAmount.Value;
+3
source

.

primitive = (decimal)offenceAmount;
+1
 primitive = offenceAmount ?? 0;
+1

Source: https://habr.com/ru/post/1774963/


All Articles