Program Help how do I type a program to read Pension?

• Question

• I'm trying to type a pension program,  for this assignment I need to find pension with a Function

how do I write it correctly in vb program?

what I have so far is:

'------------------Function: CalculatePension() ----------------------------------------
Function CalculatePension(hours As Decimal, payRate As Decimal, grossPay As Decimal)
Dim pension As Decimal

If (hours > 30) Then : pension = ((0.061 * 30) * payRate)
Else : pension = (hours - (hours - (hours * 0.061) * payRate))
End If

pension = Utility.Round(pension, 2)

Return pension
End Function 'CalculatePension()

9.39 in the program is the PayRate

it should equal 17.18

but it keeps equalling  26.06

(p.s the reason I have The Else statement is because there are only 2 number I need it for in the list of numbers the 2 are 20.5 and 24.5 to equal their pension numbers

hours: 20.5

payRate: 7.49

pension =  9.37

---------------------------

hours: 24.5

payRate: 5.57

pension = 8.32

I'm lost on this, anyone out there that can lend me a hand on how to fix this?

Tuesday, March 21, 2017 2:33 AM

All replies

• I have try'ed several ways to do this all else has failed :(

Tuesday, March 21, 2017 2:36 AM
• Hi

As this is a school assignment, here is some coder that may help (only pointers).

1. To help you when coding, see the Options included at the top of this code.

2. You will need to get to grips with the use of various numeric Types (that is really the only thing you got wrong).

3. Type is also important in variable definition. (Dim Var As Decimal for example)

'

I used both CDec and the suffix D to tell the compiler that the number required/used is of Type Decimal.

```Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
Dim p As Decimal = CalculatePension(6D, 13.33D, 133.13D)
Stop ' examine p for result    End Sub
Function CalculatePension(hours As Decimal, payRate As Decimal, grossPay As Decimal) As Decimal
Dim pension As Decimal
If hours > 30 Then
pension = CDec(0.061 * 30 * payRate)
Else
pension = CDec(hours - (hours - (hours * 0.061) * payRate))
End If
Return Math.Round(pension, 2)
End Function 'CalculatePension()
End Class```

Regards Les, Livingston, Scotland

• Edited by Tuesday, March 21, 2017 3:00 AM
Tuesday, March 21, 2017 2:57 AM