When going from a double to a decimal, presuming my double
can be represented as a decimal
...
Is it more appropriate to cast a double as a decimal:
(Explicit Numeric Conversions Table) (Note that this entry erroneously contradicts the former!)
double foo = bar;
decimal foobar = (decimal)foo;
Or is it more appropriate to call the decimal
constructor:
(Decimal Constructor)
double foo = bar;
decimal foobar = new decimal(foo);
I tend to use the decimal
constructor, but I wonder reasons to use one versus the other.
From what I can tell the only difference is that an explicit cast of a double below Decimal.MinValue
returns a zero, while using the constructor throws an OverflowException
EDIT: @Joren in the comments below notes that the cast throws an OverflowException
for small values as well... so apparently the MSDN documentation is incorrect.
Related Questions:
- C# cast a double variable to decimal
- Automatically cast from double to decimal safely: Is the following safe?