While InStr(Expression, " ") > 0 Expression = Replace(Expression, " ", " ") Wend ' Replace foot units. Sign = Sgn(Val(Expression)) ' Trim double spacing. ' Public Function ParseFeetInches( _ ByVal Expression As String) _ As Variant Dim ReplaceSets(20, 1) As String Dim ExpressionParts As Variant Dim ExpressionOneParts As Variant Dim Sign As Variant Dim DecimalInteger As Variant Dim DecimalFraction As Variant Dim DecimalInches As Variant Dim Index As Integer Dim Character As String Dim FeetInches As String Dim ExpressionOne As String Dim ExpressionOneOne As String Dim ExpressionOneTwo As String Dim ExpressionTwo As String Dim Numerator As Long Dim Denominator As Long ' Read sign. ' Negative values will only be read as such, if the first ' non-space character is a minus sign. ' For unparsable expressions, zero is returned. ' Returns the parsed values as decimal inches. ' The inch part can contain a fraction or be decimal. ' Parse a string for a value of feet and/or inches. Please study the in-line comments for the detailed operation: The final function consists of two main parts: first the clean-up, then the calculation where the integer part and the fraction (decimal) part are added to build a normal decimal number. This must be taken care of separately, because a division will force Eval to return a Double, and a Double has much less precision than a Decimal - and a later conversion to a Decimal won't help.Īn example will clearly illustrate, that using Double would reduce the count of decimals dramatically:įraction = CDec(Eval("77/4048")) Fraction -> 0.0190217391304348 Fraction = CDec("77") / CDec("4048") Fraction -> 0.0190217391304347826086956522 Remove everything else, so Eval can evaluate the final string.įurther, as in the example expression above, a division may be included as the last part. Replace any expected character or word with some math operator, and
Decimal fraction converter inches full#
Of course, much more replacements must be prepared for - in fact, 21, and too many to discuss in full - but the principle remains:
Luckily, VBA contains a "joker" function, Eval, which can do wizardry, as it can take a formula - created as a string - and evaluate it using common math.įor example, the expression 1' 5-1/4" we can figure out equals 1 * 12 + 5 + 1/4 = 17.25. While the calculation is easy, the tricky part is to get hold of the parts, as such expressions come in many varieties, for example: This is where we split an expression like 17' 2-7/8" in its parts and then calculate the decimal value. Parse feet, inches, and a fraction to a decimal
Obviously, tools are needed to convert such values, indeed when they represent very large or very small values. However, it takes at least pen and paper to convert, say, 3523.5602. It's not a simple task to figure out its decimal value of 3991.22200012207 inches.Ĭonverting in the reverse direction - from decimal inches to feet, inches, and fraction - is similar.Ī simple value like 28.125 inches can quickly be split: Such values you can convert to decimal inches using your head only:įor a total of 32.75 inches. The most common usage is for feet and inches like 2' 8" and often a fraction is present, like: 3' 5-3/4".