Decimal To Fraction conversion

Started by mmulvenna, June 04, 2008, 12:59:29 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

mmulvenna

If anyone is interested is is some code I "googled" and modifed to convert decimals to fractions. we work entirely in inches and fractions so the value entered is that   i.e. 28.125  wil return 28 1/8. If you need to work with feet and inches you will have to make some modifications

Public Sub testdec()
Dim x As Double
   x = InputBox("enter value")
   convertDecimal2Fraction (x)
End Sub

Public Sub convertDecimal2Fraction(x As Double)
     
     ' this VBA macro will convert a decimal value passed
     ' to a fraction
     ' It uses the concept of continued fractions to calculate an approximate
     ' fraction for your decimal. Useful resources about the continued fractions
     ' concept are available at :
     ' http://www.mcs.surrey.ac.uk/Personal...i/cfINTRO.html and
     ' http://homepage.smc.edu/kennedy_john/CONFRAC.PDF
     
   
    Dim z As Double
    Dim fraDen As Double
    Dim fraNum As Double
    Dim preDen As Double
    Dim preNum As Double
    Dim counter
    Dim scratchvalue As Double
    Dim intwhole As Integer
   
     
   
    intwhole = Fix(x)
    x = x - intwhole
    z = x
    preDen = 0
    fraDen = 1
    counter = 0
     
    Do
        z = (1 / (z - Int(z)))
        scratchvalue = fraDen
        fraDen = Int(z) * fraDen + preDen
        preDen = scratchvalue
        fraNum = Int(x * fraDen + 0.5)
         
        counter = counter + 1
    Loop Until (Abs((x - (fraNum / fraDen))) < 1E-20) Or (z = Int(z)) Or (counter = 594)
     
    MsgBox (intwhole & " " & fraNum & "/" & fraDen)
     
End Sub

Visio Guy

Thanks mmulvenna! Good stuff.

On a related note, Visio ships with Dimension Line shapes that can do this as well, but not everyone knows it.

Look for these stencils under File > Shapes > Visio Extras:



  • Dimensioning - Architectural (US Units)
  • Dimensioning - Engineering (Metric)
  • Dimensioning - Engineering (US Units)

The shapes essentially all have the same capabilities, but with different pre-sets.

If you right-click and choose: Precision & Units, you can opt to use Feet - Inch which will give fractional results in x/2, x/4, x/8, x/16, x/32, etc.

They ARE missing pure fractional inches (ie: can't have 55 3/8", you get 4' 7 3/8" ), which somebody was looking for at one point (mmulvenna perhaps?)

I thought that somebody posted a modified dimension line, but I can't find it. I even started working on one. Oh well, one of these days.


For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

mmulvenna

Chris,

Yes it was me.

Also, the code above needs some error checking added to it.

In addition the only way I can get the dimension lines you mention to do inches only rather than feet and inches is to change the value of the shape sheet "user.feet" to "0". The dimension the displays inches witrh fractions rather that feet.

Thanks
Mike