I am not sure why you are worried about doubles coming through instead of Decimals but you could just assign with a ternary like this
var B = A1 == null ? null : A1;
This way B
will be the same type as A1
Most of the time a Method or Function that takes a Decimal value should also take a Double value, Doubles are smaller than Decimals
Single, Double, Decimal are kind of like Integer types
- Single = 4 bytes (short = 2 bytes)
- Double = 8 Bytes (int = 4 bytes)
- Decimal = 12 bytes (long = 8 bytes)
Note: Assuming that decimal values are necessary
You could just do the conversion after you assign to the variable (after checking for null).
You might be able to assign straight across actually
decimal B = A1 == null ? null : A1;
but you need a nullable decimal (decimal?
) Another Assumption
Instead of assigning null to the Decimal you have to assign it's default value using default(decimal?)
which I assume comes out as null.
decimal? B = A1 == null ? default(decimal?) : Convert.ToDecimal(A1);
Google and MSDN are awesome resources to figure stuff like this out.
(as well as StackOverflow answersStackOverflow answers)
playing around with this if you don't want a nullable Decimal you can also write it like this
decimal C = A1 == null ? default(decimal) : Convert.ToDecimal(A1);