Dial in Decimal to Fraction (1 Viewer)

dgaller

Registered User.
Local time
Today, 01:17
Joined
Oct 31, 2007
Messages
60
Thanks to a helpful post and run by Rawb I have been using the below code.

It works great with only one issue, I am hoping to get help with. If the field I am converting is a whole number it still converts it. (i.e if the answer is 1 this returns 1-1/1)

Is there way to correct this? I would assume there is a way to put at the beginging if Whole number exit function?

Code:
Public Function Getfraction(Number As Double, Optional denominator As Long = 32, Optional Spacer As String = "-") As Variant
  ' This function returns a fractional representation of a decimal number.

  ' Number = The decimal representation of the number you want to convert.
  '   Required.
  ' Denominator = The max value of the denominator that will be accepted after
  '   conversion. Will round the number as necessary to find the nearest
  '   acceptable value. It ignored, will default to 64ths.
  ' Spacer = The spacing character to be used between the integral and
  '   fractional values of the converted number. Typically a space " " or dash
  '   "-". If ignored, will default to a dash.

  Dim nbrGCD As Long
  Dim nbrNumerator As Long

  Getfraction = -1 ' Initial value!

  If Int(Number) = Number Then
    Getfraction = CStr(Number)
  Else
    ' Round the number to the closest possible value (within the limitations of
    ' our specified Denominator)
    nbrNumerator = CLng((Number - Int(Number)) * denominator)

    ' Find the GCD of our (rounded) Numerator and Denominator numbers
    nbrGCD = GetGCD(nbrNumerator, denominator)

    ' Just in case, specify 1 as the lowest possible GCD
    If nbrGCD <= 0 Then
      nbrGCD = 1
    End If

    ' Use the GCD to reduce our fraction to the "lowest" possible value
    nbrNumerator = nbrNumerator / nbrGCD
    denominator = denominator / nbrGCD

    ' Return the resulting fractional number (as a String)
    If Int(Number) = 0 Then
      Getfraction = CStr(CLng(nbrNumerator)) & "/" & denominator
    Else
      Getfraction = CStr(Int(Number)) & Spacer & CStr(CLng(nbrNumerator)) & "/" & denominator
    End If
  End If
End Function

Public Function GetGCD(Number1 As Long, Number2 As Long) As Long
  Dim nbrN1 As Long
  Dim nbrN2 As Long
  Dim nbrTmp As Long

  nbrN1 = Number1
  nbrN2 = Number2

  Do While nbrN1
    If nbrN1 < nbrN2 Then
      nbrTmp = nbrN1
      nbrN1 = nbrN2
      nbrN2 = nbrTmp
    End If

    nbrN1 = nbrN1 Mod nbrN2
  Loop

  GetGCD = nbrN2
End Function
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:17
Joined
Aug 30, 2003
Messages
36,140
Worked fine for me in a brief test. This line should be doing what you describe:

If Int(Number) = Number Then

My guess would be that since Number was defined a Double, you may be running into a floating point issue. What exact number are you having this issue with?
 

dgaller

Registered User.
Local time
Today, 01:17
Joined
Oct 31, 2007
Messages
60
The decimal I see it with is 5.999 returns 5-1/1.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:17
Joined
Aug 30, 2003
Messages
36,140
I'm confused; you said it was converting a whole number. 5.999 is not a whole number. If you want it to convert 5.999, pass it a denominator big enough to handle it:

?Getfraction(5.999,1000)
5-999/1000
 

dgaller

Registered User.
Local time
Today, 01:17
Joined
Oct 31, 2007
Messages
60
For the function the changes it to a decimal so I can do the math to work I have the fields set to decimal. Could that be the issue?
 

dgaller

Registered User.
Local time
Today, 01:17
Joined
Oct 31, 2007
Messages
60
Sorry. It seems to be working. I think I had a case of looking at something to long.

Just to explain better, my input was 5.9375 because the DB adds .0625 for waste. That calc would give me a whole number (6) in the field that the function is converting. That is were I got the return of 6 1/1.

However after tinkering with some of the settings it seems to work.

Thanks for your help.
 

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:17
Joined
Aug 30, 2003
Messages
36,140
No problem.
 

Users who are viewing this thread

Top Bottom