Calculation issue

ProcalX

Member
Joined
Sep 28, 2005
Messages
7
Programming Experience
1-3
I have a drop down combobox, the user selects (for example) "Feet to Metres", the user enters an amount in feet into a text box and then upon clicking the convert button will calculate the conversion.

This calculation works fine if I entered in "6ft" or "5ft" but not if I enter in 6.2 (ie 6'2") as there are 12 inches per 1 feet.

How would I overcome this?

VB.NET:
Private Sub btnImpConv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImpConv.Click
	'Imperial
        Dim feet, result As Double

        'Feet to Metres
        If cmbImp.SelectedItem = "Feet to Metres" Then
            feet = CDbl(txtiInput.Text)
            result = feet * 0.3048
            lblOutput.Text = "Metres: " + result.ToString()
        End If
End Sub

Private Sub frmConv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Imperial
        cmbImp.Items.Add("Feet to Metres")
End Sub
 
Find the conversion factor for inches to centimeters == 1 inch = 2.54 cm.
Then I would parse everything out to inches do the conversion to centimeters and convert the centimeters to meters.

It might be prudent to force the format of the entered text (only can be entered as 6'2" or 6ft2in) or provide separate boxes for feet and inches. This would limit the amount of code you would need to write to parse and validate.
 
Back
Top