Essej
Essej

Reputation: 861

Multiply number with decimal C# beginner

So i'm very new at C# and programming in general and I have come across a problem i'm not able to find via google.

I got a user input that can be either a integer or a decimal which is stored in the variabel price.

But I'm not able to multiply (the value of)price with 0,12 or 0,25 decimal numbers that is to say. Here is my code.

            VAT = price *Decimal.(0,12);

VS says it wants and identifier, so I guess the problem is that I have to tell VS that it is decimals but I don't know how.

Sorry if this is really a stupid question but I just have to know.

Upvotes: 0

Views: 503

Answers (2)

Brian du Plessis
Brian du Plessis

Reputation: 21

I would recommend making sure the VAT is also decimal, otherwise you will lose the decimal part of the result (if VAT is integer).

furthermore you do not need to convert 0.12 to decimal, you can use it as is, or put 'm' behind it to indicate that it is infact decimal.

it would then look like this:

VAT = price * 0.12m

Upvotes: 1

MarcinJuraszek
MarcinJuraszek

Reputation: 125620

Use m to make numeric constant treated as decimal:

VAT = price * 0.12m;

Upvotes: 4

Related Questions