# Using Common Visual Basic Maths Conversion Functions—Imperial to Metric

### WEBINAR:On-Demand

Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Being a newbie can be hard sometimes, especially if your tasks involve Imperial to metric conversions. It has always baffled me as to why these types of tasks are common practice to programming lecturers and teachers.

I used to teach classes full time; now, I do on a part-time basis. I have always hated maths. To me, solving these types of equations on a student's first day in the world of programming has always been stupid, to be frank.

Be that as it may, once you have grown as a programmer or even if you are a seasoned programmer, some formulas or calculations can become quite frustrating quite fast. I am not a maths guy, but I have encountered situations where I had to know a formula off-hand and quickly had to learn it.

## Our Project

Open Visual Studio and create a new Visual Basic Windows Forms project. Once the project has been created, add 18 (eighteen) buttons to your form. My design looks like Figure 1:

Figure 1: Our design

## Convert Fahrenheit to Celsius and Celsius to Fahrenheit

Add the following two functions and their implementations:

```   Function FahrenheitToCelsius(dblFahrenheit As Double) As Double

Return (dblFahrenheit - 32) * 5 / 9

End Function

Function CelsiusToFahrenheit(dblCelsius As Double) As Double

Return dblCelsius * 9 / 5 + 32

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) _
Handles Button1.Click

MessageBox.Show(FahrenheitToCelsius(95).ToString())

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) _
Handles Button2.Click

MessageBox.Show(CelsiusToFahrenheit(35).ToString())

End Sub
```

## Convert Feet to Metres and Metres to Feet

Add the functions and their implementations, as follows:

```   Function FeetToMeters(ByVal dblFeet As Double) As Double

Return dblFeet * 0.3048

End Function

Function MetersToFeet(ByVal dblMeters As Double) As Double

Return dblMeters / 0.3048

End Function

Private Sub Button3_Click(sender As Object, e As EventArgs) _
Handles Button3.Click

MessageBox.Show(FeetToMeters(132).ToString())

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) _
Handles Button4.Click

MessageBox.Show(MetersToFeet(40).ToString())

End Sub
```

## Convert Inches to Centimetres and Centimetres to Inches

Add the following code to convert inches to centimetres and centimetres to inches:

```   Function InchesToCentimeters(ByVal dblInches As Double) As Double

Return dblInches * 2.54

End Function

Function CentimetersToInches(ByVal dblCentimeters As Double) _
As Double

Return dblCentimeters / 2.54

End Function

Private Sub Button5_Click(sender As Object, e As EventArgs) _
Handles Button5.Click

MessageBox.Show(InchesToCentimeters(20).ToString())

End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) _
Handles Button6.Click

MessageBox.Show(CentimetersToInches(50).ToString())

End Sub
```

## Convert Miles to Kilometres and Kilometres to Miles

```   Function MilesToKilometers(ByVal dblMiles As Double) _
As Double

Return dblMiles * 1.6093

End Function

Function KilometersToMiles(ByVal dblKilometers As Double) _
As Double

Return dblKilometers / 1.6093

End Function

Private Sub Button7_Click(sender As Object, e As EventArgs) _
Handles Button7.Click

MessageBox.Show(MilesToKilometers(20).ToString())

End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) _
Handles Button8.Click

MessageBox.Show(KilometersToMiles(32).ToString())

End Sub
```

## Convert Pounds to Kilograms and Kilograms to Pounds

```   Function PoundsToKilograms(ByVal dblPounds As Double) As Double

Return dblPounds * 0.4536

End Function

Function KilogramsToPounds(ByVal dblKilograms As Double) _
As Double

Return dblKilograms / 0.4536

End Function

Private Sub Button9_Click(sender As Object, e As EventArgs) _
Handles Button9.Click

MessageBox.Show(PoundsToKilograms(20).ToString())

End Sub

Private Sub Button10_Click(sender As Object, e As EventArgs) _
Handles Button10.Click

MessageBox.Show(KilogramsToPounds(9).ToString())

End Sub
```

## Convert Yards to Meters and Meters to Yards

```   Function YardsToMeters(ByVal dblYards As Double) As Double

Return dblYards * 0.9144

End Function

Function MetersToYards(ByVal dblMeters As Double) As Double

Return dblMeters / 0.9144

End Function

Private Sub Button11_Click(sender As Object, e As EventArgs) _
Handles Button11.Click

MessageBox.Show(YardsToMeters(10).ToString())

End Sub

Private Sub Button12_Click(sender As Object, e As EventArgs) _
Handles Button12.Click

MessageBox.Show(MetersToYards(10).ToString())

End Sub
```

## Convert Ounces to Grams and Grams to Ounces

```   Function OuncesToGrams(ByVal dblOunces As Double) As Double

Return dblOunces * 28.349

End Function

Function GramsToOunces(ByVal dblGrams As Double) As Double

Return dblGrams / 28.349

End Function

Private Sub Button13_Click(sender As Object, e As EventArgs) _
Handles Button13.Click

MessageBox.Show(OuncesToGrams(100).ToString())

End Sub

Private Sub Button14_Click(sender As Object, e As EventArgs) _
Handles Button14.Click

MessageBox.Show(GramsToOunces(100).ToString())

End Sub
```

## Convert Imperial Gallons to Litres and Vice Versa

```   Function GallonsToLiters(ByVal dblGallons As Double) As Double

Return dblGallons * 3.785

End Function

Function LitersToGallons(ByVal dblLiters As Double) As Double

Return dblLiters / 3.785

End Function

Private Sub Button15_Click(sender As Object, e As EventArgs) _
Handles Button15.Click

MessageBox.Show(GallonsToLiters(50).ToString())

End Sub

Private Sub Button16_Click(sender As Object, e As EventArgs) _
Handles Button16.Click

MessageBox.Show(LitersToGallons(50).ToString())

End Sub
```

## Convert Pints to Liters and Vice Versa

```   Function PintsToLiters(ByVal dblPints As Double) As Double

Return dblPints * 0.473

End Function

Function LitersToPints(ByVal dblLiters As Double) As Double

Return dblLiters / 2.113

End Function

Private Sub Button17_Click(sender As Object, e As EventArgs) _
Handles Button17.Click

MessageBox.Show(PintsToLiters(50).ToString())

End Sub

Private Sub Button18_Click(sender As Object, e As EventArgs) _
Handles Button18.Click

MessageBox.Show(LitersToPints(50).ToString())

End Sub
```

## Conclusion

It is always good to have a few default formulas up your sleeve, just in case you forget them. I hope this list was useful to you. If you can think of more formulas that I may have left out, please do not hesitate to contact me.

#### Hannes DuPreez

Hannes du Preez is an ex MVP for Visual Basic from 2008 to 2017. He loves technology and loves Visual Basic and C#. He loves writing articles and proving that Visual Basic is more powerful than what most believe. You are most welcome to reach him at: ojdupreez1978[at]gmail[dot]com

## Related Articles

• There are no comments yet. Be the first to comment!

• You must have javascript enabled in order to post comments.