Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting metric to imperial with fractions? 1

Status
Not open for further replies.

EagleTempest

Technical User
Jun 15, 2004
125
0
0
CA
VB6

Any easy suggestions on how to convert metric distance input to imperial with fractions?

for example if a user enters 771mm the conversion display 30 3/8
 
Dim LNewUnit As Double
Dim SNewUnit As String
LNewUnit = MetricAmount * ConversionNumber 'fill in these
For i = 1 To Len(Str(LNewUnit))
If Mid(Str(LNewUnit), i, 1) = "." Then
TempWholeNumber = CLng(Mid(Str(LNewUnit), 1, i - 1))
TempDecimaL = CDbl(Mid(Str(LNewUnit), i, Len(Str(LNewUnit)) - i + 1))
GoTo NumberSeparated
End If
Next i
TempWholeNumber = LNewUnit
TempDecimaL = 0
NumberSeparated:
If TempDecimaL <> 0 Then
TempFractNum = 0
TempFractDen = 1
For Den = 1 To 100
For Num = 1 To 100
If Abs((Num / Den) - TempDecimaL) < Abs((TempFractNum / TempFractDen) - TempDecimaL) Then
TempFractNum = Num
TempFractDen = Den
End If
Next Num
Next Den
TempFraction = TempFractNum & "/" & TempFractDen
End If
SNewUnit = TempWholeNumber & " " & TempFraction & " " & Chr(34) 'chr(34) is a quatation mark
MsgBox SNewUnit


It's not great (as it uses loops) but its a start. Good luck.
 
Millimeters to inches = divide by 25.4

dblInch = dblMM/25.4

To get the integer part of dblInch:

intInch = Int(dblInch)

and the fractional part:

dblFractInch = dblInch - intInch

Then just round to nearest 1/8.

intEighth = Int(0.5 + 8 * dblFractInch))



________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top