How can I convert a Number.Double Access column value to a Decimal?

Since decimal is preferable to double for monetary values (see David's answer here), I am trying to change my code to use decimal instead of double. IOW, I changed this:

public class InventoryItem
{
. . .
    public double UnitCost { get; set; }
    public double UnitList { get; set; }
    public double OpenQty { get; set; }
. . .

...to this:

public class InventoryItem
{
. . .
    public decimal UnitCost { get; set; }
    public decimal UnitList { get; set; }
    public decimal OpenQty { get; set; }
. . .

But that gave me grief in other code, namely "Cannot implicitly convert type 'double' to 'decimal'. An explicit conversion exists (are you missing a cast?)" in this code:

// I changed "double openQty" to "decimal openQty"
decimal openQty = (oleDbD8aReader["open_qty"] is DBNull
                                ? 0.00
                                : Convert.ToDouble(oleDbD8aReader["open_qty"]));

So, I tried changing the "Convert.ToDouble()" to "Convert.ToDecimal()" but that errors out with, "Type of conditional expression cannot be determined because there is no implicit conversion between 'double' and 'decimal'"

The data being retrieved is from MS Access, where the columns under discussion are of type Number and, more specifically, Double (Number.Double, as it were).

How can I coerce/convert Access Double to the "more better" Decimal type?


The problem was not with the ".ToDecimal()" but with the "0.00"

This:

decimal openQty = (oleDbD8aReader["open_qty"] is DBNull
    ? 0.00
    : Convert.ToDecimal(oleDbD8aReader["open_qty"]));

...had to become this:

decimal openQty = (oleDbD8aReader["open_qty"] is DBNull
    ? 0.00M
    : Convert.ToDecimal(oleDbD8aReader["open_qty"]));

(simply appending the "M" to the "0.00" shut up the compiler critics.

链接地址: http://www.djcxy.com/p/89388.html

上一篇: 身高不填页

下一篇: 如何将Number.Double Access列值转换为Decimal?