View Full Version : Convert Metric to Fractional Inches


TLW
01-23-2002, 02:28 PM
I need help converting millimeters to inches with remainders as fractions. Can anyone supply me with an equation or easy way to accomplish this task. Any help is greatly appreciated.

KKilfoil
01-24-2002, 04:57 AM
When you say 'fractions', do you want EXACT fractions, or approximations? Waht precison should these approximations be?
Must they be reduced to lowest numbers?

For example, if you use 25.4 as your conversion factor, 100mm = 3.976378 inches. Depending on precision and reduction, this could be
4,
3 and 98/100 (note that this could be 49/50)
3 and 15/16,
3 and 119/127,
etc.

KKilfoil
01-24-2002, 05:22 AM
Try the following function, which requires two arguments, the original value, and the precision of the fraction (expected to be a power of two such as 4, 16, 32 etc):

Option Compare Database
Option Explicit


Public Function MakeFraction(Num As Double, Precision As Long) As String

Dim IntegerPart As Long, Numerator As Long, Denominator As Long
Dim DecimalPart, TempVar As Double

IntegerPart = Int(Num)
DecimalPart = Num - IntegerPart
Numerator = DecimalPart * Precision ' a little round off error here!
Denominator = Precision
While (Numerator / 2 = Int(Numerator / 2))
Numerator = Numerator / 2
Denominator = Denominator / 2
Wend
MakeFraction = Str(IntegerPart) & " " & Str(Numerator) & "/" & Str(Denominator)

End Function

KKilfoil
01-24-2002, 05:51 AM
I need to slow down. Here it is again with a bit of error-checking:

Option Compare Database
Option Explicit


Public Function MakeFraction(Num As Double, Precision As Long) As String

Dim IntegerPart As Long, Numerator As Long, Denominator As Long
Dim DecimalPart As Double

If Precision <> 0 Then
IntegerPart = Int(Num)
DecimalPart = Num - IntegerPart
Numerator = DecimalPart * Precision ' a little round off error here!
Denominator = Precision
While (Numerator / 2 = Int(Numerator / 2))
Numerator = Numerator / 2
Denominator = Denominator / 2
Wend
If Numerator = Denominator Then ' it rounded off to the next higher integer
MakeFraction = Str(IntegerPart + 1)
Else
MakeFraction = Str(IntegerPart) & " " & Str(Numerator) & "/" & Str(Denominator)
End If
Else
MakeFraction = "Bad precision value!"
End If

End Function

TLW
01-24-2002, 08:15 AM
I need the fractions to the nearest 1/16 of an inch. I am assuming the reply you posted is a Module. So, which part of the module do I need to insert into my statement when building the query/report? Thanks for all of your help so far.

KKilfoil
01-24-2002, 08:51 AM
Take all of the code from my previous post and put them into a module. It doesn't much matter which one. I tend to create a one extra module and put all of my public function defs there.

Be sure to compile and save all modules.

From your query/form/report, you should be able to call MakeFraction(YourNumber,16) like you would use any intrinsic Access function, such as Int() and Format().

For example, assuming a field called 'mm' with a number in millimeters, you could define a field in a query as follows:

inches: = MakeFraction( [mm] / 25.4, 16)

to arrive at 'fractional inches' to the nearest sixteenth.

TLW
01-24-2002, 10:03 AM
Thanks for the explanation. I really appreciate your help on this and I will give it a try.

Alvin
07-10-2003, 11:14 AM
KKilfoil-
I'm a new user. I tried your function to convert decimals to fractions and when I ran the compiler I got a syntax error message referrring to the following line

If Precision <> 0 Then

I can't get past this. Any suggestions?

raskew
07-10-2003, 04:09 PM
The If Precision <> 0 Then
translates to
If Precision <> 0 Then

...but having made that correction, you still won't get the correct answer. For example (from the debug window).

? MakeFraction(.100,16)
0 1/ 8

Try copying/pasting this pair of functions to a new module, then testing with:
? MeterToFeet(.100, 16)
...which returns
0', 3-15/16"

Function MeterToFeet(inmeters As Double, Optional multiplx As Variant) As String
'*******************************************
'Name: MeterToFeet (Function)
'Purpose: Converts meters to feet, inches and
' fractions of inches
'Calls: Function ZFraction2()
'To Test: From debug window: ? MeterToFeet(15)
'Output: 49', 2-35/64"
'*******************************************

Const conMetToInches As Double = 39.3700787401575
Dim feet As Single, inches As Single, fractionx As Double
Dim itemhold As String

multiplx = IIf(IsMissing(multiplx) = True, 64, multiplx)
inches = inmeters * conMetToInches
feet = Int(inches / 12)
inches = inches - (feet * 12)
fractionx = inches - Int(inches)
inches = Int(inches)
MeterToFeet = LTrim(Str(feet)) & "', " & LTrim(Str(inches))
'Note that you may specify the degree of accuracy in the next line.
MeterToFeet = MeterToFeet & "-" & zfraction2(fractionx, multiplx) & """"
End Function

Function zfraction2(zdec As Double, Optional multiplx As Variant) As String
'*******************************************
'Name: zfraction2 (Function)
'Purpose: Converts input into a fraction
' accurate to the nearest multiplx
'Created by: raskew
'Inputs: From debug window: ? zfraction2(.874, 64)
' Default if multiplx not specified = 64
'Output: 0.874=7/8

'*******************************************

Dim Num As Integer, denom As Integer, i As Integer
Dim j As Integer, AtWork As Boolean
'create a fraction, expressed in 64ths
'num = numerator
multiplx = IIf(IsMissing(multiplx) = True, 64, multiplx)
Num = CInt(multiplx * zdec) 'the CInt function rounds up or down
'denom = denominator
denom = multiplx

'reduce the fraction
AtWork = True
Do
j = Num
For i = j To 2 Step -1
If Num Mod i = 0 And denom Mod i = 0 Then
Num = Num / i
denom = denom / i
j = Num
Exit For
End If
Next i
AtWork = False
Loop While AtWork = True

'express the results as a string
zfraction2 = LTrim(Str(Num)) & "/" & LTrim(Str(denom))
End Function

Alvin
07-11-2003, 05:15 AM
raskew-
Thanks for the response. I copied and pasted your "zfraction2" function into a new module. When I ran it I got a "run time error '6', overflow.

My table field values are in decimal form, and I'm trying to get them to show as fractions on the report I produce for my shop personnel. When I enter your function, I get the error message.
examples of field values are 37.5, 21.625, etc.

Any further suggestions?

KKilfoil
07-11-2003, 05:43 AM
My function was NOT intended to do Imperial to Metric unit conversions.

Its purpose was to convert a given value to a fraction.

So if you want to convert meters to inches expressed as a fraction, you need to call it like:
TheInches: =MakeFraction([YourMeterValue]*39.37,16)
If you want the fraction rounded to the nearest 16th.

HTH

raskew
07-11-2003, 05:52 AM
Alvin-

In posting the functions, I managed to duplicate Function MeterToFeet(). I've corrected the posting and tested it by copying/pasting the forum copy.

Once you've got it straight try this from the debug window:

? metertofeet(37.5, 16)
123', 0-3/8"

Bob

KKilfoil
07-11-2003, 05:59 AM
raskew:

If you read Alvin's post, I'm not sure that he wants to do any metric conversions at all. I think he merely wants to convert a decimal value to a fractional one.

Therefore, I think all he needs is my function, or
your zfraction2 routine.
i.e:

[YourFraction]: =zfraction2(.874) returns "7/8".


{edited to fix boneheaded mistake!}

Alvin
07-11-2003, 11:42 AM
KKilfoil-

Your MakeFraction formula does the trick. I had a hard time figuring out where to use it, but I finally put it in a query to make the calculations on my table data. I never could get it to work in the report. Anyway, thanks for the assistance. Raskew's formula works also, but didn't give me the format of integer/fraction I was looking for.

Alvin
07-14-2003, 05:19 AM
KKilfoil -

I'm back with another problem. Seems that when I use the MakeFration funtion, if the number I try to convert is a whole number with no decimal value (ie - 10), the system crashes. I ran the debug and get the following results

?makefraction(10.125,16) yields 10 1/8
?makefraction(10.03125,16) yields 10 1/16
?makefraction(10.999,16) yields 11
All these are fine, but when I try anything less than a decimal of .03125 down to no decimal, I crash. Iocks up the whole Access program. Seems like it rounds up fine, but not down.

The real problem fro me is where there is no decimal part of the original number. Any suggestions?

Thanks

KKilfoil
07-14-2003, 05:32 AM
I never thought about that possibility....

You need to add another If statement to work around numerator=0, because my present fraction reduction loop is stuck in an endless loop (since zero/2 is always = zero).


try this...

Before the 'While' statement, add

If Numerator<>0 then

After the 'Wend' statement, add

End If


This will eliminate the logic error you pointed out.

Alvin
07-14-2003, 06:30 AM
KKilfoil-

Here's my results

?makefraction(10.125,16)
10 1/ 8
?makefraction(10.03125,16)
10 0/ 16
?makefraction(10,16)
10 0/ 16
?makefraction(10.999,16)
11

You extra lines prevent the crash,but how can I get rid of 0/16? Sorry to be so dumb.

KKilfoil
07-14-2003, 06:59 AM
Where I had:

Else
MakeFraction = Str(IntegerPart) & " " & Str(Numerator) & "/" & Str(Denominator)
End If


Use this:
Else
MakeFraction = Str(IntegerPart)
If Numerator <> 0 then
MakeFraction = MakeFraction & " " & Str(Numerator) & "/" & Str(Denominator)
End If
End If

Alvin
07-14-2003, 07:06 AM
KKilfoil-

Your a gentleman and a scholar, thanks for all prompt help.