-2
subTotal += carts.Amount * product.Price;

Cannot implicitly convert type 'int?' to 'double'. An explicit conversion exists (are you missing a cast?)

mason
  • 31,774
  • 10
  • 77
  • 121
Rashid Khan
  • 11
  • 1
  • 1
  • You need to get the value of the nullable (if it has one). – SLaks Oct 31 '16 at 16:48
  • You should not be using doubles for monetary code. Use [decimal](https://msdn.microsoft.com/en-us/library/364x0z75.aspx). – mason Oct 31 '16 at 17:16
  • An explicit conversion exists. Did you forget to include a cast in your expression? It really is not clear what you're asking. There are lots of answers on Stack Overflow already discussing converting from `int` to `double` and converting from a nullable value to a non-nullable value (see marked duplicate). If you have a question not answered in those many other posts, please explain: provide a good [mcve] showing what you've tried, and describe _specifically_ what it is you've tried and what you're having trouble with. – Peter Duniho Oct 31 '16 at 17:54

3 Answers3

1

Here

subTotal += carts.Amount * product.Price;

you try to implicitly convert an int? to an int (carts.Amount). Solution:

if (carts.Amount.HasValue) {
    subTotal += carts.Amount.Value * product.Price;
} else {
    //carts.Amount is null, handle it
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
-1

You can use Convert.ToDouble(x) where x is the int value. Check out https://msdn.microsoft.com/en-us/library/ayes1wa5(v=vs.110).aspx for more information (this is C#, but its similar in the other Cs)

Adam Wells
  • 545
  • 1
  • 5
  • 15
-2

int? nullableIntVal = 100; var dblVal = Convert.ToDouble(nullableIntVal.GetValueOrDefault(0));