Re: Convert decimal to fraction string



This is probably an idiotic way to get there...
but since I love public humiliation so much <g> here goes my stab at it
....

given a decimal value I want to get a string representation of it's
fractional equivalent
99% of the time I'll be working with "architectural" units eg 1/16, 3/32,
7/64, 3 3/16, etc so my testing below was rather limited

Rick will probably have a one-liner for this or someone will point out a
built in vb function<g>

in any case, observations, corrections appreciated

If it helps you any, here is a response along with a function (actually, two
working in tandem) that I posted a few years back.

Rick

Consider this answer from a previous post of mine (remember, this was an
answer to someone else's question; hence the reference to the number
7.25489)...

Try this function. You can specify the maximum denominator to use in the
optional 2nd parameter. If you leave it out, then a default value of 64 is
used. The function rounds, either up or down, to the *closest* fraction
having the unit of resolution set by the 2nd argument. So, your value of
7.25489 will round to 7-1/4 even though the decimal part is more than 1/4
(it is closer to 1/4 than it is to 17/64, the next fraction past 1/4 using
the 1/64 unit-of-resolution). By the way, the routine does not limit you to
using denominators that are powers of two (although those are probably the
only ones that make sense). That means, if you specify a 2nd parameter of
47, you will receive 7-12/47 as an answer.

If your version of VB only supports optional arguments that are Variants
(and can't be defaulted), then you will have to modify the function
definition accordingly and set the default value of 64 inside of your
function. If your version of VB doesn't support optional arguments at all,
then simply turn it into a required argument.

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


.



Relevant Pages

  • Re: Converting decimal to fractions
    ... Dim TopNumber As Integer ... Dim Numerator As Integer ... Dim Denominator As Integer ... If DecimalNumber < 0 Then ...
    (comp.lang.basic.realbasic)
  • Re: Converting decimal to fractions (Revised Code)
    ... Still make the function's return type a String. ... Dim TopNumber As Integer ... Dim Numerator As Integer ... Dim Denominator As Integer ...
    (comp.lang.basic.realbasic)
  • Re: Rundungsproblem mit "Fix"
    ... Einerseits benötige ich die Zahl ohne Nachkommastellen, ... Dim sValue As String ... Dim Numerator As Double ... Dim Denominator As Double ...
    (microsoft.public.de.vb)
  • Re: Conflicting needs for __init__ method
    ... single integer, another Rational instance, and perhaps floats, Decimal ... And when initializing from a pair of integers---a numerator ... sure that the denominator is positive. ... like negation or raising to a positive integer power, ...
    (comp.lang.python)
  • Re: infinite product
    ... This result generalizes to products with an arbitrary number of numerator and denominator terms. ... Note that this does not always give interesting formulae, since both sides may be zero because of poles of Gamma. ... It is for this reason that it is advisable to keep x in formula. ...
    (sci.math)

Loading