Marzdor
Marzdor

Reputation: 79

Doing math between 2 lists

I get this error.

operator * cannot be applied to operands of type double and decimal

and when I looked how to fix it there was add a suffix m or M (tried didn't work)

        double[] statsBase = { 708, 2.83, 288, 3.3, 63, 0.9, 10, 20, 350, 180, 900 };

        double[] statsPerLvl = { 52, 0.21, 10, 0, 2.93, 0, 1.5, 2.5, 0, 0, 0 };

        double[] statsWithLvl = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    for (int num = 0; num <= 10; num++)
{
    statsWithLvl[num] = statsBase[num] + (statsPerLvl[num] * numericUpDown_level.Value);
}

Upvotes: 0

Views: 85

Answers (4)

Luc Morin
Luc Morin

Reputation: 5380

Simply do this:

statsWithLvl[num] = statsBase[num]
    + (statsPerLvl[num] * (double)numericUpDown_level.Value);

Upvotes: 2

Mihai Hantea
Mihai Hantea

Reputation: 1743

You can't multiply a decimal by a double. You can fix this by type casting.

The type decimal was designed to be useful for financial calculations since it offers high precision at the cost of reduced range for the size of the type in bytes.

You can stop using double and use decimal instead.

MSDN:

If you want a numeric real literal to be treated as decimal, use the suffix m or M

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54447

A suffix is only relevant on a literal. You have no literals. You need to convert the Value property, which is type Decimal, to a Double in order to multiply it with another Double. Use Convert.ToDouble for the purpose.

Upvotes: 0

bubbinator
bubbinator

Reputation: 495

I am assuming that statWithLvl and statsBase are doubles as there is no error for the + operator. The problem is that the numericUpDown's Value property is a decimal. If my assumption is correct, then the simple fix is to cast the numericUpDown's Value property.

Upvotes: 0

Related Questions