×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# display decimal as a fraction

## display decimal as a fraction

(OP)
how can i display 12.5 (a calculated value, long int) as 12 1/2 in an access report
Replies continue below

### RE: display decimal as a fraction

are you only looking to do simple fractions (1/2, 1/4, 1/10, 1/100) or need something a little more detailed?

"If I were to wake up with my head sewn to the carpet, I wouldn't be more surprised than I am right now."

### RE: display decimal as a fraction

Ask in one of the specialist Access forums on this site. Use the 'find a forum' search at the top of this page.

___________________________________________________________
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
Steam Engine Prints

### RE: display decimal as a fraction

I pulled this off the web and then wrapped my function around it because the function returns improper fractions.

#### CODE

Function dec2frac(dblDecimal As Double) As String
'
'   Excel function to convert decimal values to integer fractions.
'
'   Written by:     Erik Oosterwal
'   Started on:     November 16, 2006
'   Completed on:   November 16, 2006
'

Dim intNumerator, intDenominator, intNegative As Long   ' Declare integer variables as long
'   integers.
Dim dblFraction, dblAccuracy As Double                  ' Declare floating point variables
'   as double precision.
Dim txtDecimal As String                                ' need a string representation of the input value
Dim i As Integer                                                       ' in order to determine the required accuracy.

' Find the accuracy needed for the output by checking the number of digits behind the decimal point
'   of the input value.
'
'    dblAccuracy = 1 / 10 ^ (Len(CStr(dblDecimal - Fix(dblDecimal))) - 2)
'
'   While the formula above should work, there is a serious error in the way Excel handles
'   decimal numbers and there's a huge rounding error issue.  Subtracting the int() of
'   12.1 from 12.1 produces 0.0999999999 or something similar.  Obviously that won't
'   work for our desired accuracy of the magnitude of the fractional part of the number
'   so a slower more cumbersome method has to be used...

dblAccuracy = 0.1                                       ' Set the initial Accuracy level.
txtDecimal = CStr(dblDecimal)                           ' Get a  string representation of the input number.

For i = 1 To Len(txtDecimal)                                ' Check each character to see if it's a decimal point...
If Mid\$(txtDecimal, i, 1) = "." Then                    ' if it is then we get the number of digits behind the decimal
dblAccuracy = 1 / 10 ^ (Len(txtDecimal) - i + 1)    '   assign the new accuracy level, and
Exit For                                            '   exit the for loop.
End If
Next

intNumerator = 0                                ' Set the initial numerator value to 0.
intDenominator = 1                              ' Set the initial denominator value to 1.
intNegative = 1                                 ' Set the negative value flag to positive.

If dblDecimal < 0 Then intNegative = -1         ' If the desired decimal value is negative,
'   then set the negative value flag to
'   negative.

dblFraction = 0                                 ' Set the fraction value to be 0/1.

While Abs(dblFraction - dblDecimal) > dblAccuracy   ' As long as we're still outside the
'   desired accuracy, then...
If Abs(dblFraction) > Abs(dblDecimal) Then      ' If our fraction is too big,
intDenominator = intDenominator + 1         '   increase the denominator
Else                                            ' Otherwise
intNumerator = intNumerator + intNegative   '   increase the numerator.
End If

dblFraction = intNumerator / intDenominator     ' Set the new value of the fraction.

Wend

dec2frac = LTrim(Str(intNumerator)) & "/" & LTrim(Str(intDenominator)) ' Display the numerator and denominator

End Function

Public Function properDec2Frac(dblVal As Double) As String
Dim intPart As Long
Dim decPart As Double
intPart = Fix(dblVal)
decPart = dblVal - intPart
If intPart = 0 And decPart = 0 Then
properDec2Frac = "0"
ElseIf intPart = 0 Then
properDec2Frac = dec2frac(decPart)
Else
properDec2Frac = intPart & " " & dec2frac(Abs(decPart))
End If
End Function
call properDec2Frac.  I tested with

-12.5
-.5
0
.5
12.5

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!