INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Mathematical Formula2

 Forum Search FAQs Links MVPs

## Mathematical Formula

(OP)
Hello all.

I have a formula that calculates a price based on the number of miles.

Here is the formula:

For the first 1 mile - charge £19 flat rate
For the next 29 miles - charge £1.50 for every 1 mile
For the next 20 miles - charge £1.25 for every 1 mile
For the rest - charge £1.00 for every 1 mile

Here are some valid results for testing:

1 mile = £19
5 miles = £25
10 miles = £32.50
20 miles = £47.50
50 miles = £87.50
100 miles = £137.50

I think this can be acheived using a Select Case.

I have tried the following:

#### CODE

<%

Mileage = 10

Sub GetPrice(ByVal Mileage As Integer)
Select Case Mileage

Case Is <=1
Price = 19

Case Is 1 To 30
Price = 19 + ((Mileage - 1) * 1.50)

End Select

End Sub

Response.Write Price

%> 

But already I'm stuck (Expected ')' on bold line..

I'm not sure if this is the best way to go about things (logically)...

Really could use some assistance.

Thanks

### RE: Mathematical Formula

Hi there,

look at the following (warning: untested!). It might not look super elegant but it should do the trick just fine:

#### CODE

price=19

Select Case Mileage

Case Is >50
for i = 51 to mileage
price=price+1
next i
price=price+63.5

Case Is >30
for i = 31 to mileage
price=price+1.25
next i
price=price+43,5

Case Is >=1
for i= 2 to mileage
price=price+1.5
next i

Case Else 'mileage=0
price=0 

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

### RE: Mathematical Formula

(OP)
Hello MakeItSo, thank you very much for the swift reply.

My actual formula is a bit more complex so I was hoping for something a little easier to read and edit.

I was hoping for a clear distinction between the prices to be charged and the additional miles to be added.

That way I could add further steps to my formula quite easily.

### RE: Mathematical Formula

A: This "Sub GetPrice(ByVal Mileage As Integer)" is Visual Basic NOT VbScript

B:This code

#### CODE --> basic

Select Case Mileage

Case Is >50
for i = 51 to mileage
price=price+1
next i
price=price+63.5

Case Is >30
for i = 31 to mileage
price=price+1.25
next i
price=price+43,5

Case Is >=1
for i= 2 to mileage
price=price+1.5
next i

Case Else 'mileage=0
price=0 

Is ALSO Visual Basic NOT VbScript

Select .. Case in VbScript can ONLY check for equality, it cannot do comparison testing

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

### RE: Mathematical Formula

(OP)
Thanks Chris, can you please advise me as to the best approach (or perhaps direct me towards a similar example) and I can try and get it working..?

### RE: Mathematical Formula

Thanks Chris. It's the result of moving away from VBS for two years....

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

### RE: Mathematical Formula

@ axLW

You are not going to get a "simple" snippet of code for what you hope to do, the vbscript in ASP does not have the capabilities to parse your data and make such 'decisions'. It is going to require a call to a very convoluted function or instantiate a object class that encapsulates several functions and properties.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

### RE: Mathematical Formula

Give me two or three hours and I'll put together a vbscript class that you can expand on.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

### RE: Mathematical Formula

Though it might take a little longer because I'll have to set up a temporary Windows machine to test on.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

### RE: Mathematical Formula

(OP)
Thanks a lot Chris. I'll be sure to send you a delicious fruit hamper in the post.

### RE: Mathematical Formula

Isn't it just a bunch of sequential if statements?

#### CODE

price = 19
distance = distance - 1
if distance > 0 then price = price + min(distance, 29) * 1.5
distance = distance - 29
if distance > 0 then price = price + min(distance, 20) * 1.25
distance = distance - 20
if distance > 0 then price = price + distance * 1

function min(a,b)
if a < b then
min = a
else
min = b
end if
end function 

### RE: Mathematical Formula

2
Okay back at it, sorry took longer than I anticipated to get ASP running on IIS7, and I had to sleep as well. (I must be getting 'old')

ASP Class encapsulation (called MileageCalcs file name mileage-calcs.asp)

#### CODE --> ASP(vbscript)

<%

class MileageCalcs

dim i_BP1
dim i_BP2
dim i_BP3
dim f_Mult1
dim f_Mult2
dim f_Mult3
dim i_IC
dim b_IsError

'Properties with only a "Let" are write only
' properties with only a ""Get"" are read only'

public Property let PriceBreak1(ByVal val)
i_BP1 = val
end property

public Property let PriceBreak2(ByVal val)
i_BP2 = val
end property

public Property let PriceBreak3(ByVal val)
i_BP3 = val
end property

public Property let Muliplier1(ByVal val)
f_Mult1 = val
end property

public Property let Muliplier2(ByVal val)
f_Mult2 = val
end property

public Property let Muliplier3(ByVal val)
f_Mult3 = val
end property
public Property let Initial (ByVal val)
i_IC = val
end property

public Property Get IsError()
IsError = b_IsError
' set true if a value of less than one is passed to .Calc
end property

public Function Calc(i_Mileage)
if i_mileage < 1 then
b_IsError = True
exit function
tmp = -i_IC
end if
i_Mileage = i_Mileage - 1
if i_Mileage > 0 then tmp = tmp + min(i_Mileage, i_BP1) * f_Mult1
i_Mileage = i_Mileage - i_BP1
if i_Mileage > 0 then tmp = tmp + min(i_Mileage, i_BP2) * f_Mult2
i_Mileage = i_Mileage - i_BP2
if i_Mileage > 0 then tmp = tmp + i_Mileage * f_Mult3
Calc=tmp + i_IC
end Function

function min(i_x,i_y)
if i_x < i_y then
min = i_x
else
min = i_y
end if
end function

public sub Initialize()

end sub
public sub terminate()

end sub
end class
%> 

Bit more flexible than a fixed function.

If you want to set any of the values when the class is instantiated you can do so in the Initialize sub

How to include and use is below;

#### CODE --> ASP(vbscript)

<!--#include virtual = "mileage-calcs.asp" -->
<%
Set calcs = new  MileageCalcs
calcs.PriceBreak1 = 29
calcs.pricebreak2 = 20
calcs.Initial = 19
Calcs.Muliplier1 = 1.5
Calcs.Muliplier2 = 1.25
Calcs.Muliplier3 = 1.0

response.write calcs.calc(100)
%> 

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

### RE: Mathematical Formula

(OP)
Chris this looks GREAT!

Let me get my teeth into it :)

### RE: Mathematical Formula

(OP)
Chris this is brilliant. Works perfectly and I've managed to add more steps.

What do I owe you?!

### RE: Mathematical Formula

Thing of beauty.

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

### RE: Mathematical Formula

#### Quote:

What do I owe you?!

A grand total of nothing at all, I don't code for financial gain these days, it's just good 'brain exercise' :)

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

#### Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!