Results 1 to 6 of 6

Thread: Math Wizard Anyone 2 ?

  1. #1

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141

    Math Wizard Anyone 2 ?

    How can I convert decimal values back to fractions...
    Ex : .75 = 3/4, 38.25 = 38 1/4 ...

    Any modules/subs/comments/suggestions much appreciated....


    Thanx in advance...

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    What you will need:


    You will need a function that is able to find factors of numbers.

    What you do:
    Easiest way is to take the decimal value and multiply it by the fraction 10^x / 10^x where x is the number of decimal places your original has in it. (Note that this gets rediculous if you have a number with 10 or so decimal places, so round it to 4 places).

    Then take the resulting fraction, and find the highest common factor of the numerator and denominator. Divide both by this number. Repeat until you cannot find any more common factors. You will have your fraction.

    Code:
    Example:
    38.25
    multiply by 100/100 (which is 1 as you know)
    = 3825 
      ----
       100
    
    highest common factor is: 25 so:
    
    3825   /   25   =  153
    ----       --      ---
     100   /   25   =    4
    
    Which is your answer of 38 1/4
    If you try this, see what happens with decimals like 3.3333333333. It will not work very well unfortunately, so what you can do is use a number like 10^x -1 instead.

    See how you get on...
    Paul Lewis

  3. #3

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141
    Thanks PaulLewis...

    humn... one more thing, any tip on how to get the highest common factor of the numerator/denominator thru code?

  4. #4
    Guest
    Here is some code I had a while back, you should be able to make it work. How it works is you put the numerator in the text1 and denomenator in text2. This just reduces the fractions and operates on the assumtion that the whole numbers have already been extracted....example: 4/13 will work, but 32/12 will not. You would need to convert 32/12 to 2 8/12 first, then run the fraction through to reduce to lowest common denominator. You would end up with 2 2/3. Good luck.


    Code:
    Dim text1 As Integer, text2 As Integer, i As Integer, HighDivNumber As Integer
    'since we know text1 will be bigger than text2 this is how to do it
    'you will need to write your own statement to make sure text1 is bigger
    text1.Text = text1
    text2.Text = text2
    For i = 1 To text2
        If (text1 Mod i) = 0 And (text2 Mod i) = 0 Then
            HighDivNumber = i
        End If
    Next i
    text1 = text1 / HighDivNumber
    text2 = text2 / HighDivNumber
    'now put these numbers in your 3rd text box

  5. #5

    Thread Starter
    Addicted Member curlywink's Avatar
    Join Date
    Mar 2000
    Location
    Manila, Philippines
    Posts
    141
    Thanks guys!

  6. #6
    Addicted Member S@NSIS's Avatar
    Join Date
    Aug 2000
    Location
    Stoke-On-Trent, England
    Posts
    243
    Hi,
    You might want to try this function that I wrote a while back. It seems to catch everything (so far ).
    Code:
    Public Function ConvertDecimalToFraction(iDecimal As Double) As String
    Dim lCount As Double
    Dim TempOdds As Double
    Dim bOK As Boolean
    Dim SplitOdds() As String
    
    On Error GoTo InverseNumber
    
    Do While bOK = False
        lCount = lCount + 1
        TempOdds = iDecimal * lCount
        If InStr(Str(TempOdds), ".") > 0 Then
            SplitOdds = Split(CStr(TempOdds), ".")
            If Mid(SplitOdds(1), 1, 3) = "000" Then
                bOK = True
            End If
        Else
            bOK = True
        End If
        
    Loop
    
    ConvertDecimalToFraction = CStr(TempOdds) & "/" & CStr(lCount)
    
    Exit Function
    
    InverseNumber:
    
    TempOdds = 1 / iDecimal
    
    If InStr(Str(TempOdds), ".") > 0 Then
        SplitOdds = Split(CStr(TempOdds), ".")
        If Mid(SplitOdds(1), 1, 3) = "000" Then
            ConvertDecimalToFraction = "1/" & CStr(CInt(TempOdds))
            Exit Function
        Else
            ConvertDecimalToFraction = "Error"
            Exit Function
        End If
    Else
        ConvertDecimalToFraction = "1/" & CStr(CInt(TempOdds))
    End If
    
    End Function
    use like this

    Code:
    dim dblDecimal As Double
    dim strFraction As String
    dblDecimal = 0.75
    strFraction = ConvertDecimalToFraction(dblDecimal)
    Msgbox strFraction
    Hope this helps

    Shaun
    Web/Application Developer
    VB6 Ent (SP5), Win 2000,SQL Server 2000

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width