Decimal to Fraction

  • Thread starter Thread starter Tubakskie
  • Start date Start date
T

Tubakskie

Guest
Help pls! I'm a newbie with MS Access, I want to know on how will I convert a decimal to fraction. For example convert 0.5 to 1/2 or 5.5 to 5 1/2! Thank you and more power!
 
Code by CyberCow

Code:
Public Function myGetFrac(nVal) ' nVal = Decimal number to be converted

' Test in debug window: ?myGetFrac(32.956)

' The result will be 32 61/64"

Dim i As Integer

Dim lVal, fVal

Dim nmr, dnmr

 
    i = InStr(nVal, ".")

    lVal = Left(nVal, i - 1)

    fVal = right(nVal, Len(nVal) - (i - 1))

    nmr = CLng((fVal * 64))

    dnmr = 64

 
    If nmr Mod 32 = 0 Then

        nmr = nmr / 32

        dnmr = 2

    End If

    If nmr Mod 16 = 0 Then

        nmr = nmr / 16

        dnmr = 4

    End If

    If nmr Mod 8 = 0 Then

        nmr = nmr / 8

        dnmr = 8

    End If

    If nmr Mod 4 = 0 Then

        nmr = nmr / 4

        dnmr = 16

    End If

    If nmr Mod 2 = 0 Then

        nmr = nmr / 2

        dnmr = 32

    End If

 
    myGetFrac = lVal & " " & nmr & "/" & dnmr & Chr(34)

    ' Remove the  & Chr(34) if you dont want the inches symbol in the result

 
End Function
 
Check out this page for a function called "MakeFraction". It appears at almost the bottom of the page, so scroll down, most of the way to the bottom to find it.

Copy the code form the page, beginning with "Function MakeFraction"... right down until you find "End Function"

Paste that code into a module, then save the module.

You can then call the function, for example in a query: e.g.

SELECT tblExamResult.StudentID, tblExamResult.ExamID, MakeFraction([ExamScore],100) AS FractionalScore, tblExamResult.ExamScore AS DecimalScore
FROM tblExamResult;

would give results like

Student ID : ExamID : FractionalScore : DecimalScore
1 : 1 : 44 19/25 : 44.76
2 : 1 : 50 1/2 : 50.5
3 : 1 : 60 23/100 : 60.23456

etc

HTH

Regards

John.
 
Tenks!

Thank you for all your replies, it works! Tenks and more power!
 

Users who are viewing this thread

Back
Top Bottom