Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Sep 28, 1998



Convert from Fractional to Decimal Numbers

While developing a database front end for hand-tool management, I discovered a need to handle both fractional and decimal representations of dimensions in the same text box. This makes it easier on users who worked from a variety of prints to input part feature sizes. To accomplish this, place this function in the LostFocus event of any text box that can receive numerical input. You can also cycle through the appropriate text boxes and run the function against the text value of each. In addition, this function only checks for the inches character (double quotes) at the end of the text string of fractional dimensions. It also looks for spaces and/or dashes between whole numbers and fractions and checks for both forward and backward slashes within fractions. It doesn't work with negative values:

 Private Function ReturnDecimalValue(Size As _
	String) As String
	Dim strSize As String
	Dim iGap As Integer
	Dim iSlash As Integer
	Dim sWhole As Single
	If Size <> "" Then Size = LTrim(RTrim(Size))
	'previous code may have stripped text to nothing 
	'if it was just spaces, so test
	If Size <> "" Then
		'strip off inch character (double 
		'quotes) if it's there
		If Right(Size, 1) = Chr$(34) Then _
			Size = Left(Size, Len(Size) - 1)
		iGap = InStr(Size, "-")
		If iGap = 0 Then iGap = InStr(Size, " ")
		If iGap Then sWhole = CSng(Left(Size, iGap - 1))
		strSize = Right(Size, Len(Size) - iGap)
		iSlash = InStr(strSize, "/")
		'user may have input backward slash 
		'in fraction instead of forward slash; 
		If iSlash = 0 Then iSlash = InStr(strSize, "\")
		'convert result to decimal form for 
		'saving in database
		If iSlash Then Size = CStr(sWhole + _
			(CSng(Left(strSize, iSlash - 1)) / _
			CSng(Right(strSize, Len(strSize) - iSlash))))
	End If
	ReturnDecimalValue = Size
End Function
Randall Arnold
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date