beny lim
beny lim

Reputation: 1304

Math calculation in vb

I am trying to perform a simple multiplication in vb.

Below is my code:

Dim minus As Integer
Dim minusPrice As Integer
Dim totalPrice As Integer
         If quantity > 20 Then
            minus = quantity - 20
            minusPrice = ((minus) * (0.3))
            MsgBox("Minus " + minus.ToString)
            MsgBox("minusPrice " + minusPrice.ToString)

            totalPrice = 30 + minusPrice
        End If

But why is "minusPrice " giving me 0? It should give me "0.3".

Upvotes: 0

Views: 1114

Answers (4)

rerun
rerun

Reputation: 25495

dim minusprice as integer 

this means that when you multiply by .3 you are doing integer multiplication. The integer part of .3 is 0 so something * 0 = 0. Change your code to.

dim minusprice as single

Upvotes: 0

perfectionist
perfectionist

Reputation: 4346

You declared minusPrice as an integer, so it has rounded your result.

You need...

Dim minusPrice As Decimal
Dim totalPrice As Decimal

or Double... or float, depending on your needs. Prices are usually decimal.

--- Edit ---

Just noticed that I missed a vital part:

minusPrice = ((minus) * (0.3m))

Unless 0.3 is also expressed as a decimal you may get small rounding errors.

Upvotes: 7

Guffa
Guffa

Reputation: 700152

As you have declared minusPrice as Integer, it can't contain fractions. The calculation is done correctly, but then it's converted to an integer.

What's happening in the code is actually:

minusPrice = CInt(CDbl(minus) * 0.3)

If you declare minusPrice as a Double or Decimal instead, the result of the calcuation won't be converted.

The type Double is used for regual floating point operations. There is an inherent limitation of precision that you should be aware of, but if you use this just for displaying the price, it's good enough.

The type Decimal is a fixed point data type, which means that it can represent fractional values exactly. This is what you should use if you need to keep track of monetary values in a serious way.

Upvotes: 0

Dennis Traub
Dennis Traub

Reputation: 51624

minusPrice is declared as an Integer which will only contain the part before the decimal point. To be able to store decimal values you need to use the type Decimal. The same goes for totalPrice.

Dim minus As Integer
Dim minusPrice As Decimal
Dim totalPrice As Decimal
If quantity > 20 Then
    minus = quantity - 20
    minusPrice = ((minus) * (0.3))
    MsgBox("Minus " + minus.ToString)
    MsgBox("minusPrice " + minusPrice.ToString)

    totalPrice = 30 + minusPrice
End If

Upvotes: 3

Related Questions