Link to home
Start Free TrialLog in
Avatar of suran78
suran78

asked on

calculating Ratio in Vbscript

How can I calculate a ratio in Vbscript.  For Example 25:15 = 5:3

Your help/sugesstion is very much needed.



Avatar of aeklund
aeklund

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
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
Avatar of suran78

ASKER

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?



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...
Avatar of suran78

ASKER


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.
 



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!
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
%>
If you still can't fix it, then I suggest increasing the points, cuz as is, your questions are beyond a 20 point question....
Avatar of suran78

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of aeklund
aeklund

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of suran78

ASKER

Thanks a lot !

Suran78
Your welcome, glad to help.