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

 Code to convert decimal numbers to mixed numbers, ... Author: snakeeyes1000 Submitted: 8/13/2002 Version: VB6 Compatibility: VB6 Category: Mathematics Views: 11752 Code to convert decimal numbers to mixed numbers, and vice versa. DecimalToMixedNumber: Convert a number like "3.8 nto "3 4/5" SimplifyFraction: Convert a fraction like "50/100" into "1/2" FractionToDecimal: Converts a fraction like "22/4" into "5 1/2". Declarations: 'none Code: Function DecimalToMixed(Number As String) As String Dim perPos As Long, wholepart As String, fracpart As String perPos = InStr(1, Number, ".") wholepart = Mid(Number, 1, perPos - 1) fracpart = Mid(Number, perPos + 1) DecimalToMixed = wholepart & " " & SimplifyFraction(fracpart & "/" & CStr(10 ^ (Len(fracpart)))) End Function Function SimplifyFraction(Fraction As String) As String Dim divPos As Long, numerator As Long, denominator As Long Dim tmp As Long, lCounter1 As Long divPos = InStr(1, Fraction, "/") numerator = Mid(Fraction, 1, divPos - 1) denominator = Mid(Fraction, divPos + 1) If numerator > denominator Then tmp = denominator Else tmp = numerator For lCounter1 = tmp To 2 Step -1 If ((numerator Mod lCounter1 = 0) And (denominator Mod lCounter1 = 0)) Then numerator = numerator \ lCounter1 denominator = denominator \ lCounter1 End If Next lCounter1 SimplifyFraction = CStr(numerator) & "/" & CStr(denominator) End Function Function FractionToDecimal(Fraction As String) As String Dim divPos As Long, numerator As Long, denominator As Long divPos = InStr(1, Fraction, "/") numerator = Mid(Fraction, 1, divPos - 1) denominator = Mid(Fraction, divPos + 1) FractionToDecimal = numerator / denominator End Function