As a manufacturer we required a fraction conversion function and not a 1/25 fraction but an actual 1/32 or 1/16 fraction, you know, one that shows up on a tape measure. I thought I would share the code that I came up with and would appreciate any critique of the code. I have two different functions, one for 1/32 and on for 1/16. I do realize that I could have simply checked the 'Accuracy' variable but there are many users who prefer the software to work in 6 or 7 places of accuracy but print in either 1/16 or 1/32 increments. I've also added a .01 to the value since I didn't want to write a extra code to look for .9999, .4999, .65, etc .... I couldn't really find a good rounding function and this worked well for our application.
Thanks in advance for your critique, I'm not terribly familiar with this language yet.
Function The32Fraction (Value as double) as string
dim WholeNumber as String
dim Numerator as integer
dim FractionPart as string
dim CompleteFractionalNumber as string
Value = Value +.01
WholeNumber = cstr(Math.Truncate(Value))
Numerator = Math.Truncate((Value-Math.Truncate(Value))*32)
Select Case Numerator
Case 1 : FractionPart = "1/32"
Case 2 : FractionPart = "1/16"
Case 3 : FractionPart = "3/32"
Case 4 : FractionPart = "1/8"
Case 5 : FractionPart = "5/32"
Case 6 : FractionPart = "3/16"
Case 7 : FractionPart = "7/32"
Case 8 : FractionPart = "1/4"
Case 9 : FractionPart = "9/32"
Case 10 : FractionPart = "5/16"
Case 11 : FractionPart = "11/32"
Case 12 : FractionPart = "3/8"
Case 13 : FractionPart = "13/32"
Case 14 : FractionPart = "7/16"
Case 15 : FractionPart = "15/32"
Case 16 : FractionPart = "1/2"
Case 17 : FractionPart = "17/32"
Case 18 : FractionPart = "9/16"
Case 19 : FractionPart = "19/32"
Case 20 : FractionPart = "5/8"
Case 21 : FractionPart = "21/32"
Case 22 : FractionPart = "11/16"
Case 23 : FractionPart = "23/32"
Case 24 : FractionPart = "3/4"
Case 25 : FractionPart = "25/32"
Case 26 : FractionPart = "13/16"
Case 27 : FractionPart = "27/32"
Case 28 : FractionPart = "7/8"
Case 29 : FractionPart = "29/32"
Case 30 : FractionPart = "15/16"
Case 31 : FractionPart = "31/32"
Case Else : FractionPart = ""
End Select
If value < 1 Then
CompleteFractionalNumber = FractionPart
Elseif FractionPart = "" Then
CompleteFractionalNumber = WholeNumber
Else
CompleteFractionalNumber = WholeNumber + "-" + FractionPart
End If
Return CompleteFractionalNumber
End Function
Function The16Fraction (Value as double) as string
dim WholeNumber as String
dim Numerator as integer
dim FractionPart as string
dim CompleteFractionalNumber as string
Value = Value +.01
WholeNumber = cstr(Math.Truncate(Value))
Numerator = Math.Truncate((Value-Math.Truncate(Value))*16)
Select Case Numerator
Case 1 : FractionPart = "1/16"
Case 2 : FractionPart = "1/8"
Case 3 : FractionPart = "3/16"
Case 4 : FractionPart = "1/4"
Case 5 : FractionPart = "5/16"
Case 6 : FractionPart = "3/8"
Case 7 : FractionPart = "7/16"
Case 8 : FractionPart = "1/2"
Case 9 : FractionPart = "9/16"
Case 10 : FractionPart = "5/8"
Case 11 : FractionPart = "11/16"
Case 12 : FractionPart = "3/4"
Case 13 : FractionPart = "13/16"
Case 14 : FractionPart = "7/8"
Case 15 : FractionPart = "15/16"
Case Else : FractionPart = ""
End Select
If value < 1 Then
CompleteFractionalNumber = FractionPart
Elseif FractionPart = "" Then
CompleteFractionalNumber = WholeNumber
Else
CompleteFractionalNumber = WholeNumber + "-" + FractionPart
End If
Return CompleteFractionalNumber
End Function