Using Common Visual Basic Maths Conversion Functions—Imperial to Metric


Building the Right Environment to Support AI, Machine Learning and Deep Learning

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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   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


   End Sub

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


   End Sub


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.

About the Author

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.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date