Click to See Complete Forum and Search --> : Number conversions
I_Compute
Jan 11th, 2000, 08:42 AM
I know that you can add an OLE to your program in VB6 linked to Excel to do a decimal to fraction conversion, but I would like to complete my program to run independent to MS Office applications.
I know that there should be some code that will convert a decimal input to a fractional resultant but I haven't been able to figure it out. Could one of you give some insight to how the code structure should look like.
I would really appreciate the assistance! (So would my customer!)
Benj
Jan 12th, 2000, 03:36 AM
This should get you started. It will convert any fraction to its lowest common denominator(not just decimal ones).
Private Sub Convert_to_Fraction()
Dim Numerator As Integer
Dim Denominator As Integer
Dim WholeNumber As Integer
Dim NoDivision As Boolean
Dim Divisor As Integer
On Error GoTo CancelErr
Numerator = InputBox("Please enter the numerator")
Denominator = InputBox("Please enter the denominator")
If Numerator >= Denominator Then
WholeNumber = Numerator / Denominator
Numerator = Numerator Mod Denominator
End If
Do Until NoDivision Or Numerator = 1 Or Numerator = 0
NoDivision = True
For Divisor = 2 To 9
If Numerator Mod Divisor = 0 And _
Denominator Mod Divisor = 0 Then
Numerator = Numerator / Divisor
Denominator = Denominator / Divisor
NoDivision = False
Exit For
End If
Next
Loop
If WholeNumber > 0 Then
If Numerator > 0 Then
MsgBox "The final number is " & WholeNumber & "and " & _
Numerator & "/" & Denominator
Else
MsgBox "The final number is " & WholeNumber
End If
Else
If Numerator > 0 Then
MsgBox "The final fraction is " & Numerator & "/" & Denominator
Else
MsgBox "The final number is 0"
End If
End If
Exit Sub
CancelErr:
End Sub
Lyla
Jan 12th, 2000, 10:17 AM
Hi I_Compute.
If you are looking for 1/4, 1/2 and 3/4 ONLY , then
there is a simple way to do it in VB.
for: 1/4 Use char(188) => ¼
for: 1/2 Use char(189) => ½
for: 3/4 Use char(190) => ¾
Good Luck.
Lyla.
[This message has been edited by Lyla (edited 01-12-2000).]
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.