Using Common Visual Basic Maths Conversion Functions—Imperial to Metric

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.

The aim of this article is to help you with those stubborn niggling formulas that you might not know immediately. Let’s convert imperial to metric and vice versa!

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:

Our design
Figure 1: Our design

Note: All I will be covering with this article is the formulas. I will not try to overcomplicate them.

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

Add the following code:

   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 DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read