?
Solved

calculating Ratio in Vbscript

Posted on 2003-03-06
12
Medium Priority
?
509 Views
Last Modified: 2010-04-07
How can I calculate a ratio in Vbscript.  For Example 25:15 = 5:3

Your help/sugesstion is very much needed.



0
Comment
Question by:suran78
  • 7
  • 4
12 Comments
 
LVL 10

Expert Comment

by:aeklund
ID: 8083170
Sounds like you need the low denominator... this should work..  The input is 2 long numbers, and the output is a string.. if you want the input to be a string too, then it would just take a little modification.

Msgbox Ratio(25, 15)

Function Ratio(num As Long, den As Long) As String
  Dim num1 As Long
  Dim num2 As Long
  Dim num3 As Long
  Dim quot As Integer

  num1 = num
  num2 = den
 
  Do
    If num2 > num1 Then
      num3 = num1
      num1 = num2
      num2 = num3
    End If
   
    quot = Int(num1 / num2)
    num1 = num1 Mod num2
 
  Loop Until num1 = 0
  Ratio = num / num2 & ":" & den / num2

End Function
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8083193
oops.. since your using vbscript you cannot declare the variables... so here is a mod version of the above to work in vbscript

Msgbox Ratio(25, 15)

Function Ratio(num, den)
 Dim num1, num2, num3, quot

 num1 = num
 num2 = den
 
 Do
   If num2 > num1 Then
     num3 = num1
     num1 = num2
     num2 = num3
   End If
   
   quot = Int(num1 / num2)
   num1 = num1 Mod num2
 
 Loop Until num1 = 0
 Ratio = num / num2 & ":" & den / num2

End Function
0
 

Author Comment

by:suran78
ID: 8083720
Hi aeklund,

Thanks a lot for at replying, I tried to run you code but every time I get this error:

Microsoft VBScript runtime error '800a000d'
Type mismatch: 'Ratio'

/rptPMvsRepair.asp, line 751

I am not sure why. do you have any clue how to correct it?



0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:aeklund
ID: 8083857
your trying to call the vbscript with asp.. you can't do that.. put the function in the asp section of the page and not in the <script> tags...
0
 

Author Comment

by:suran78
ID: 8083968

aeklund,

This is how I am using it:


<td align=right class="totalColor"><%Ratio(countPM,countRepair)%></td>


<script language="VBScript">

 
Function Ratio(num, den)
Dim num1, num2, num3, quot

num1 = num
num2 = den

Do
  If num2 > num1 Then
    num3 = num1
    num1 = num2
    num2 = num3
  End If
 
  quot = Int(num1 / num2)
  num1 = num1 Mod num2

Loop Until num1 = 0
Ratio = num / num2 & ":" & den / num2

End Function
</script>

If I put the function outside the <script> tag, I get syntax error.
Your code it working correctly when I place it is a new file which has only the msgbox and the function inside the script tags.  I don't know how to put it inside the asp+html code in my program.
Can you guide me here ? Thanks for your assistance.
 



0
 
LVL 11

Expert Comment

by:supunr
ID: 8084205
Sub Ratio(Num as long, Den as Long)
     Dim i as long
     Dim Min as long

     If (Num = Den) then
          MsgBox "Ratio = 1:1"
     Else
          if (Num > Den) then Min = Den Else Min = Num

          For i = Min to 1 Step -1
               If ((Den MOD i) = 0 And (Num MOD i) = 0) then
                    MsgBox "Ratio: " (Num / i) & ":" & (Den / i)
                    Exit For
               End If
          Next i
     End if
End Sub

Good Luck!
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8084311
Take it out of the <script> tags and put it inside asp tags...

<%
Function Ratio(num, den)
Dim num1, num2, num3, quot

num1 = num
num2 = den

Do
 If num2 > num1 Then
   num3 = num1
   num1 = num2
   num2 = num3
 End If
 
 quot = Int(num1 / num2)
 num1 = num1 Mod num2

Loop Until num1 = 0
Ratio = num / num2 & ":" & den / num2

End Function
%>
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8084353
If you still can't fix it, then I suggest increasing the points, cuz as is, your questions are beyond a 20 point question....
0
 

Author Comment

by:suran78
ID: 8102871
Hi Supunr,

Thanks for your help but your code gave me gave the Type mismatch error. I am still trying to use it by putting it outside the script tag.  Hopefully I won't get the same error this time.


Hi aeklund,

Thansk for reccomending to put the function out of the script tags.  It does work , but there is another error:

Microsoft VBScript runtime error '800a000b'

Division by zero

/rptPMvsRepair.asp

I have increased the points.  I was not aware that it will get complicated.

Thanks for you help,
Suran
0
 
LVL 10

Accepted Solution

by:
aeklund earned 400 total points
ID: 8103497
Suran-

I never took into consideration that you would have an ratio of 15:0 or 0:15, but in case you do, I added logic to handle that..

<%

response.write Ratio(25,15)
response.write Ratio(15,3)
response.write Ratio(4,0)

Function Ratio(num, den)
Dim num1, num2, num3

num1 = num
num2 = den

Do
  If num2 > num1 Then
    num3 = num1
    num1 = num2
    num2 = num3
  End If
     
     If num1 = 0 or num2 = 0 Then
          Ratio = CStr(num & ":" & den)
          Exit Function
     End If
  num1 = num1 Mod num2

Loop Until num1 = 0

Ratio = CStr(num / num2 & ":" & den / num2)

End Function

%>
0
 

Author Comment

by:suran78
ID: 8103523
Thanks a lot !

Suran78
0
 
LVL 10

Expert Comment

by:aeklund
ID: 8103595
Your welcome, glad to help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question