Visual Basic .Net Rounding Issue-Collection of common programming errors
I am building a unit converter with Visual Basic and Visual Studio 2012. I am using the conversion rate of 1 inch = 0.0833333 feet for the inch to foot conversion. When I type in 12 inches, it gives me an answer of 0.9999996 instead of 1. How can I fix this problem?
The issue is with the inch to foot conversion below.
' converts inch to...
If cbo1.SelectedIndex = 3 Then
If cbo2.SelectedIndex = 0 Then
' meter
txtUnit2.Text = (dblUnit1 * 0.0254).ToString.Trim
ElseIf cbo2.SelectedIndex = 1 Then
' millimeter
txtUnit2.Text = (dblUnit1 * 25.4).ToString.Trim
ElseIf cbo2.SelectedIndex = 2 Then
' foot
txtUnit2.Text = (dblUnit1 * 0.0833333).ToString.Trim
ElseIf cbo2.SelectedIndex = 3 Then
' inch
txtUnit2.Text = txtUnit1.Text
End If
End If