Find Code: All Words Any of the Words Exact Phrase Home   :   Code   :   Forums   :   Submit   :   Mailing List   :   About   :   Contact
 Code All VB.NET ASP.NET C# VB Classic ASP Classic Snippets Popular Resources Submit Code Forums Articles Tips Links Books Contest Link to us

 Decimal to Inch 64ths Fractions Author: Bob Brandt E-mail: Click to e-mail author Submitted: 11/21/2015 Version: VB 2010 Compatibility: VB 2005, VB 2008, VB 2010 Category: Mathematics Views: 4631 Converts decimal numbers to Inch fractions, in increments of 64's 32's 16's 8's 4's 2's 0.02 of an Inch accuracy. Declarations: ''''Call From Sub Dim DecimalNumber As Double Dim Results As String = "" DecimalNumber = 0.24 '''Example If DecimalNumber > 0 Then Results = Dec_To_Inch_Frac(DecimalNumber) End If ''Examples 0.24 to Results = 15 / 64 Ex: 0.25 to Results = 1 / 4 Code: Private Function Dec_To_Inch_Frac(ByVal DecimalNumber As Double) Dim Vaules_1 As Double Dim Vaules_2 As Double Dim Vaules_3 As Double Dim Vaules_4 As Double Dim Results As String = "" Dim i As Integer Dim Multiplier As Integer Vaules_1 = 128 Vaules_2 = Val(DecimalNumber) * 128 Do Multiplier = Multiplier + 1 Vaules_3 = Vaules_1 * Multiplier Vaules_4 = Vaules_2 * Multiplier Loop Until Multiplier > 5000 Or _ (Math.Abs(Int(Vaules_3) - Vaules_3) < 0.01 And Math.Abs(Int(Vaules_4) - Vaules_4) < 0.01) ReDivide: For i = 2 To 129 If Int(Vaules_1 / i) - (Vaules_1 / i) = 0 _ And Int(Vaules_2 / i) - (Vaules_2 / i) = 0 Then Vaules_1 = (Vaules_1 / i) Vaules_2 = (Vaules_2 / i) GoTo ReDivide End If Next i Vaules_1 = Int(Vaules_1) Vaules_2 = Int(Vaules_2) If Vaules_1 > 64 And Vaules_2 > 1 And (Vaules_1 <> 64 Or Vaules_1 <> 32 Or Vaules_1 <> 16 Or Vaules_1 <> 8) Then Vaules_2 = Int(Vaules_2 / 2) Vaules_1 = Int(Vaules_1 / 2) End If If Vaules_1 <> 64 And Vaules_1 <> 32 And Vaules_1 <> 16 And Vaules_1 <> 8 And Vaules_1 <> 4 And Vaules_1 <> 2 And Vaules_1 <> 3 And Vaules_1 <> 5 And Vaules_1 <> 10 Then Select Case Vaules_1 Case Is > 32 Vaules_2 = 64 * ((Vaules_2 / Vaules_1)) Vaules_1 = 64 Case Is > 16 Vaules_2 = 32 * ((Vaules_2 / Vaules_1)) Vaules_1 = 32 Case Is > 8 Vaules_2 = 16 * ((Vaules_2 / Vaules_1)) Vaules_1 = 16 Case Is > 4 Vaules_2 = 8 * ((Vaules_2 / Vaules_1)) Vaules_1 = 8 Case Is > 2 Vaules_2 = 4 * ((Vaules_2 / Vaules_1)) Vaules_1 = 4 End Select End If If Vaules_1 = 10 Then Vaules_1 = 64 Vaules_2 = Int(Vaules_2 * 6.4) End If TryAgain: If (Vaules_2 / 2) = Int(Vaules_2 / 2) And (Vaules_1 / 2) = Int(Vaules_1 / 2) Then Vaules_2 = Vaules_2 / 2 Vaules_1 = Vaules_1 / 2 GoTo TryAgain End If If Vaules_2 - Int(Vaules_2) > 0.49999 Then Vaules_2 = Int(Vaules_2) + 1 Else Vaules_2 = Int(Vaules_2) End If Results = CStr((Vaules_2) & " / " & (Vaules_1)) Return Results End Function