"Steve Gerrard" <no*************@comcast.net> wrote in message

news:X_********************@comcast.com...

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message

news:24********************@comcast.com... I'm not so sure I understand the input mechanism you want to use;
but, before you get started coding, you might want to take a look at the

function at this link...

http://vbnet.mvps.org/code/helpers/numbertofraction.htm

I don't know if you were aware of this or not. If you try

MakeFraction(1.99, 32), you will get 1 1/1. I would suggest adding

these lines after the do loop:

If Denominator = 1 Then

WholeNumber = WholeNumber + 1

MakeFraction = CStr(WholeNumber)

Else...

Nope, I wasn't aware of that flaw; thanks for pointing it out. Although

I'd note that 1 1/1 is technically correct (1+1/1=2); but, of course,

nobody wants to see it written that way. By the way, there are lots of

input that will degenerate to this odd format... the argument pairs

(3.97,16), (3.98,16), (3.99,16) for example. While your change would

work, I'm inclined to try and catch the problem earlier on. I'm thinking

adding this

If Numerator = Denominator Then

Numerator = 0

WholeNumber = WholeNumber + 1

End If

immediately after this line

Numerator = Format(Denominator * _

Abs(DecimalNumber - WholeNumber), "0")

would also work and it would avoid the Do-Loop too. I haven't looked at

that code in some time and I obviously developed it before I was aware

of the problems with IsNumeric. So, I'd also propose changing that test

line too (I've added my standard IsNumber function so that it can be

called instead of VB's IsNumeric function). Here is the final

"corrected" code I'm thinking of sending on to Randy; do you (or anyone

else following this thread) see any problem with it?

Rick - MVP

Function MakeFraction(ByVal DecimalNumber As Variant, _

Optional ByVal LargestDenominator As Long = 64, _

Optional bShowDash As Boolean = False) As String

Dim GCD As Long

Dim TopNumber As Long

Dim Remainder As Long

Dim WholeNumber As Long

Dim Numerator As Long

Dim Denominator As Long

If IsNumber(DecimalNumber) Then

DecimalNumber = CDbl(DecimalNumber)

WholeNumber = Fix(DecimalNumber)

Denominator = LargestDenominator

Numerator = Format(Denominator * _

Abs(DecimalNumber - WholeNumber), "0")

If Numerator = Denominator Then

Numerator = 0

WholeNumber = WholeNumber + 1

End If

If Numerator Then

GCD = LargestDenominator

TopNumber = Numerator

Do

Remainder = (GCD Mod TopNumber)

GCD = TopNumber

TopNumber = Remainder

Loop Until Remainder = 0

Numerator = Numerator \ GCD

Denominator = Denominator \ GCD

MakeFraction = CStr(WholeNumber) & _

IIf(bShowDash, "-", " ") & _

CStr(Numerator) & "/" & _

CStr(Denominator)

Else

MakeFraction = CStr(WholeNumber)

End If

Else

'Input wasn't a number, handle error here

End If

End Function

Function IsNumber(ByVal Value As String) As Boolean

' Leave the next statement out if you don't

' want to provide for plus/minus signs

If Value Like "[+-]*" Then Value = Mid$(Value, 2)

IsNumber = Not Value Like "*[!0-9.]*" And _

Not Value Like "*.*.*" And _

Len(Value) > 0 And Value <> "." And _

Value <> vbNullString

End Function