Fractions calculation

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Does anyone know how to do fractions calculation in Access since there isn't
any data type as fractions. There isn't a problem in Excel. Please help.
 
What are you trying to do?

A fraction is simply division. 1/3 is 1 divided by 3, or .333333333

What is it you are trying to accomplish, and how is it that you do it in
Excel?

Rick B
 
Try the eval function:

eval("1/3") returns 0.33333333333333

If you want to do something like a fraction multiplication e.g. 2/3 * 1/4
.... then if you just want the result in decimal then you could do this by
using

eval("2/3*1/4") or eval("2/3")*eval("1/4)

If you want the result of that to actually show as "1/6" then you have two
options - neither of them wildly straightforward -

1) Store the fraction in 2 fields... for 2/3 one for the 2 and one for the
3. You could then do the calculation by doing the 2*1 and the 3*4. But
then you'd have to find a routine to reduce the 2/12 result to 1/6

2) Calculate the result as a decimal and then use a function to convert that
back to a fraction.

I've never tried to do either of the last two, so you'd be on own trying to
figure out exactly how to do it.
 
....though I've had a play now and come up with a couple of things - this
function will take a string along the lines of 2/6 and reduce it to 1/3...

Function reduce(t)
If IsNull(t) Then
reduce = ""
Exit Function
End If
'num is the numerator
'den is the denominator
'pos is the position of the /
Dim num As Long, den As Long, pos As Integer
pos = InStr(t, "/")
If pos = 0 Then
reduce = t
Exit Function
End If
num = CLng(Left(t, pos - 1))
den = CLng(Mid(t, pos + 1))
Dim i As Long
'maxfactor is the maximum value that it's going to be possible to divide
'both top and bottom by... the maximum of the numerator and the integer
'part of half of the denominator
Dim maxfactor As Long
maxfactor = num
If Int(den / 2) < maxfactor Then
maxfactor = Int(den / 2)
End If
For i = maxfactor To 2 Step -1
If num Mod i = 0 And den Mod i = 0 Then
num = num / i
den = den / i
Exit For
End If
Next
If num Mod den = 0 Then
reduce = num / den
Else
reduce = CStr(num) & "/" & CStr(den)
End If
End Function

And then this function takes a decimal and converts it to it's simplest
fraction....

Function dectofrac(d As Double) As String
'Multiply d by increasing values... when the result is an integer
'(or close to one enough to just be a rounding error)
'then that multiplier is going to be the denominator of the fraction.

'MaxDen is the largest denominator that can be coped with
'Its value would be determined by the actual data in use
Dim MaxDen
MaxDen = 10000000
Dim i As Long
For i = 1 To MaxDen
If IsInt(d * i) Then
Exit For
End If
Next
If i = MaxDen + 1 Then
dectofrac = "Not found"
Else
If i = 1 Then
dectofrac = CStr(Round(d * i, 0))
Else
dectofrac = (CStr(Round(d * i, 0)) & "/" & CStr(i))
End If
End If
End Function

....which uses this...

Function IsInt(d As Double) As Boolean
'determines whether d is an integer value... or very close to an integer
value to
'allow for rounding errors
IsInt = Abs(d - Round(d, 0)) < 0.00000000001
End Function

Those need some error handling added, and I'm sure I'm missing some
exceptions (e.g. division by zero type stuff) but they seem to work for me.
 
Back
Top