A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Setting Up & Running Reports
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Convert demicals to fraction



 
 
Thread Tools Display Modes
  #1  
Old May 18th, 2004, 04:11 AM
JP
external usenet poster
 
Posts: n/a
Default Convert demicals to fraction

In ac2003 I have a field in a report that diplays 23.5625 and I need to convert it to read 23 9/16 how do you convert from numbers to a text.
  #2  
Old May 18th, 2004, 04:19 AM
Cheryl Fischer
external usenet poster
 
Posts: n/a
Default Convert demicals to fraction

Access MVP Arvin Meyer created the FractionIT() function to do just that.
The code is below; simply copy it into a public module and call it from your
query or in a report.

Public Function FractionIt(dblNumIn As Double) As String
'================================================= ===================
' Name: FractionIt
' Purpose: Converts a double into a string representing a rounded fraction
' Inputs: dblNumIn As Double
' Returns: String
' Author: Arvin Meyer
' Date: June 22, 2001
' Comment: Rounds down from 1/64 over
'================================================= ===================
On Error GoTo Err_FractionIt

Dim strFrac As String
Dim strSign As String
Dim strWholeNum As String
Dim dblRem As Double

If dblNumIn 0 Then
strSign = "-"
dblNumIn = dblNumIn * -1
Else
strSign = " "
End If

strWholeNum = Fix([dblNumIn])

dblRem = [dblNumIn] - [strWholeNum]

Select Case dblRem
Case 0
strFrac = ""
Case Is 0.046875
strFrac = "1/32"
Case Is 0.078125
strFrac = "1/16"
Case Is 0.109375
strFrac = "3/32"
Case Is 0.140625
strFrac = "1/8"
Case Is 0.171875
strFrac = "5/32"
Case Is 0.203125
strFrac = "3/16"
Case Is 0.234375
strFrac = "7/32"
Case Is 0.265625
strFrac = "1/4"
Case Is 0.296875
strFrac = "9/32"
Case Is 0.328125
strFrac = "5/16"
Case Is 0.359375
strFrac = "11/32"
Case Is 0.390625
strFrac = "3/8"
Case Is 0.421875
strFrac = "13/32"
Case Is 0.453125
strFrac = "7/16"
Case Is 0.484375
strFrac = "15/32"
Case Is 0.515625
strFrac = "1/2"
Case Is 0.546875
strFrac = "17/32"
Case Is 0.578125
strFrac = "9/16"
Case Is 0.609375
strFrac = "19/32"
Case Is 0.640625
strFrac = "5/8"
Case Is 0.671875
strFrac = "21/32"
Case Is 0.703125
strFrac = "11/16"
Case Is 0.734375
strFrac = "23/32"
Case Is 0.765625
strFrac = "3/4"
Case Is 0.796875
strFrac = "25/32"
Case Is 0.828125
strFrac = "13/16"
Case Is 0.859375
strFrac = "27/32"
Case Is 0.890625
strFrac = "7/8"
Case Is 0.921875
strFrac = "29/32"
Case Is 0.953125
strFrac = "15/16"
Case Is 0.984375
strFrac = "31/32"
Case Is 1
strFrac = "1"
End Select

If strFrac = "1" Then
FractionIt = strSign & (strWholeNum + 1)
Else
FractionIt = strSign & strWholeNum & " " & strFrac
End If

Exit_FractionIt:
Exit Function

Err_FractionIt:
Select Case Err
Case 0

Case Else
MsgBox Err.Description
Resume Exit_FractionIt
End Select

End Function

--

Cheryl Fischer, MVP Microsoft Access



"JP" wrote in message
...
In ac2003 I have a field in a report that diplays 23.5625 and I need to

convert it to read 23 9/16 how do you convert from numbers to a text.


  #3  
Old May 18th, 2004, 06:09 AM
fredg
external usenet poster
 
Posts: n/a
Default Convert demicals to fraction

On Mon, 17 May 2004 20:11:03 -0700, JP wrote:

In ac2003 I have a field in a report that diplays 23.5625 and I need to convert it to read 23 9/16 how do you convert from numbers to a text.


Paste this function into a module:

Public Function DecimalToFrac(DecimalIn) As String

'Convert decimal to Fraction

Dim strWholePart As String
Dim varNumerator As Variant
Dim lngDenominator As Long
Dim intX As Integer
strWholePart = Int(DecimalIn)
intX = InStr([DecimalIn], ".")

If intX = 0 Or IsError(Mid([DecimalIn], intX + 1)) Then
DecimalToFrac = strWholePart
Exit Function
End If

varNumerator = Mid(DecimalIn, InStr(DecimalIn, ".") + 1)
lngDenominator = 1 & String(1 * Len(varNumerator), "0")

Do While lngDenominator Mod 5 = 0 And varNumerator Mod 5 = 0
varNumerator = varNumerator / 5
lngDenominator = lngDenominator / 5
Loop

Do While lngDenominator Mod 2 = 0 And varNumerator Mod 2 = 0
varNumerator = varNumerator / 2
lngDenominator = lngDenominator / 2
Loop

DecimalToFrac = strWholePart & " " & varNumerator & "/" &
lngDenominator

End Function

Call it from a query:
ExpecimalToFrac([FieldName])
--
Fred
Please only reply to this newsgroup.
I do not reply to personal email.
 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:19 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.