|
-
Jun 20th, 2005, 10:58 PM
#1
Thread Starter
New Member
Calculations [VB6] [RESOLVED]
I am writing a program for a friend - here are the details for how the pricing works - how do i do this in VB as i am not very good with calculations etc in vb
Minimum Charge or $5, which covers 10 kgs for 5 kms
Each kg over 10 kgs incurs a charge of $0.50
Distances over 5kms incur an additional charge of $1.00 per km up to 10 kms
For deliveries over 10 kms, the extra distance should be rounded up to the nearest 5 kms and charged at $2.00 for each extra 5 km block – e.g. if the total distance is 23.75 kms, the extra charge is $5.00 for kms 5 to 10, plus $2.00 x 3(13.75/5) = $11.00
IntWeight = package weight
IntDistance = distance
Thanks
Last edited by nzguy2004; Jun 22nd, 2005 at 10:42 PM.
-
Jun 21st, 2005, 12:12 AM
#2
Re: Calculations [VB6]
I think this is what you want. Let me know if it's not right.
VB Code:
Private Sub Command1_Click()
Dim Weight As Integer, Distance As Integer, Charge As Currency
Dim AddedUnits As Integer
Weight = Val(Text1.Text)
Distance = Val(Text2.Text)
Select Case Weight
Case 0 To 10
Charge = 5
Case Else
Charge = 5 + (0.5 * (Weight - 10))
End Select
Select Case Distance
Case Is < 5.001
' No Extra Charge
Case 5.001 To 10
Charge = Charge + Distance - 5 * 1
Case Is > 10
' round to nearest 5
If Distance / 5 = Int(Distance / 5) Then
AddedUnits = (Distance - 10) / 5
Else
AddedUnits = Distance - 10 ' 23.75 - 10 = 13.75
AddedUnits = (AddedUnits + 5 - (AddedUnits Mod 5)) / 5
' 13.75 + 5 <18.75> - 13.75 Mod 5 <3.75> = 15 / 5 = 3
End If
Charge = Charge + (AddedUnits * 2)
End Select
Text3.Text = Format(Charge, "Currency")
End Sub
Private Sub Form_Load()
Text1.Text = "" ' Weight
Text2.Text = "" ' Distance
Text3.Text = "" ' Cost
End Sub
Last edited by dglienna; Jun 21st, 2005 at 12:22 AM.
-
Jun 21st, 2005, 12:33 AM
#3
Thread Starter
New Member
Re: Calculations [VB6]
How would i modify it so that the output was to lblWeight, Distance, TotalCost
and it got its data from intDistance and intWeight?
-
Jun 21st, 2005, 12:45 AM
#4
Re: Calculations [VB6]
Something like this
VB Code:
Weight = intWeight
Distance = intDistance
' Do Calcs
'
lblWeight = Weight
lblTotalCost = Format(Charge, "Currency")
lblDistance = Distance
-
Jun 21st, 2005, 12:47 AM
#5
Re: Calculations [VB6]
if intweight and intdistance are interger, they will always round down, where you really want to always round up, if it is 10.2 it is over 10 so sould be 11
VB Code:
Dim intweight As Single, intdistance As Single rwt as sin
Dim rwt As Single, rdist As Single, wcharge As Single, dcharge As Single, charge As Single
charge = 5
intdistance = 23.75
rdist = intdistance \ 1
'Round UP
If rdist < intdistance Then rdist = rdist + 1
intweight = 10.5
rwt = intweight \ 1
If rwt < intweight Then rwt = rwt + 1
Select Case rwt
Case Is <= 10: wcharge = 0
Case Is > 10: wcharge = 0.5 * (rwt - 10)
End Select
Select Case rdist
Case Is <= 5: dcharge = 0
Case 5 To 10: dcharge = 1 * (rdist - 5)
Case Is > 10: dcharge = 5 + (rdist - 10) \ 5 * 2
End Select
charge = charge + wcharge + dcharge
MsgBox "Weight = " & intweight & ": Distance = " & intdistance & ": Total charge = " & charge
pete
-
Jun 21st, 2005, 12:49 AM
#6
Thread Starter
New Member
Re: Calculations [VB6]
Thankyou to both of you. I will try both when i get a chance and let you know
-
Jun 22nd, 2005, 01:36 AM
#7
Thread Starter
New Member
Re: Calculations [VB6]
Here is an example which neither code worked for, or are my calculations incorrect?
Weight:33 Distance:23
Excess Weight Cost:11.50
18kms (5 to 10 kms @ 1.00/km) 5.00
13 kms = 6.00
Total:27.50
Program: 22.50
-
Jun 22nd, 2005, 03:37 AM
#8
Re: Calculations [VB6]
ok fixed that, try some more to make sure no other bugs
VB Code:
Dim intweight As Single, intdistance As Single
Dim rwt As Single, rdist As Single, wcharge As Single, dcharge As Single, charge As Single
charge = 5
intdistance = 23
rdist = intdistance \ 1
'Round UP
If rdist < intdistance Then rdist = rdist + 1
intweight = 33
rwt = intweight \ 1
If rwt < intweight Then rwt = rwt + 1
Select Case rwt
Case Is <= 10: wcharge = 0
Case Is > 10: wcharge = 0.5 * (rwt - 10)
End Select
Select Case rdist
Case Is <= 5: dcharge = 0
Case 5 To 10: dcharge = 1 * (rdist - 5)
Case Is > 10
ed = (rdist - 10) \ 5
If ed * 5 < rdist Then ed = ed + 1
dcharge = 5 + ed * 2
End Select
charge = charge + wcharge + dcharge
MsgBox "Weight = " & intweight & ": Distance = " & intdistance & ": Total charge = " & charge
pete
-
Jun 22nd, 2005, 03:41 AM
#9
Thread Starter
New Member
Re: Calculations [VB6]
Perfect Thankyou very much
I think that lblTotalCost = Format(charge, "Currency")
Will add the decimal places etc?
-
Jun 22nd, 2005, 04:02 AM
#10
Re: Calculations [VB6]
Yes, to your localization settings on the computer.
-
Jun 22nd, 2005, 10:38 PM
#11
Thread Starter
New Member
Re: Calculations [VB6]
Thanks to westconn1 and dglienna
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|