?
Solved

calculating Ratio in Vbscript

Posted on 2003-03-06
12
Medium Priority
?
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Technology Partners: 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!

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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 Month10 days, 8 hours left to enroll

765 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