Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleCalculate mileage and fuel consumption in the United States, United Kingdom, and metric units in Visual Basic 6
DescriptionThis example shows how to calculate mileage and fuel consumption in the United States, United Kingdom, and metric units in Visual Basic 6,
Keywordsmathematics, mileage, fuel consumption, metric, liters, miles per gallon, milometers per liter, example, example program, Windows Forms programming, Visual Basic 6, VB 6
CategoriesAlgorithms
 

People in the United States, Canada, the United Kingdom, and several other places measure a car's fuel efficiency in miles per gallon. As you might expect, countries that use the metric system may measure fuel efficiency in kilometers per liter, but many of them also measure a car's fuel consumption in liters per 100 kilometers. To make matters even more confusing, the United Kingdom, Canada, and others use an imperial gallon, which is different from an American gallon, so you can't easily compare values in miles per gallon across different countries.

This example calculates fuel efficiency in all three systems: US miles per gallon, UK miles per gallon, and liters per 100 kilometers. The program uses the following code to perform its calculations.

 
' Conversion factors.
Private Const miles_per_km As Single = 0.621371192
Private Const us_gallons_per_liter As Single = _
    0.264172052637296
Private Const uk_gallons_per_liter As Single = 0.2199692483

' Calculate the mileage statistics.
Private Sub cmdCalculate_Click()
Dim miles As Single
Dim gallons As Single
Dim us_mpg As Single
Dim us_gal_per_100_miles As Single
Dim uk_gals As Single
Dim uk_mpg As Single
Dim uk_gal_per_100_miles
Dim liters As Single
Dim kilometers As Single
Dim km_per_liter As Single
Dim l_per_100_km As Single

    ' Get the inputs.
    miles = Val(txtDistance.Text)
    gallons = Val(txtFuel.Text)

    ' Convert to miles and US gallons.
    If (optKilometers.Value) Then miles = miles * _
        miles_per_km
    If (optUkGallons.Value) Then
        gallons = gallons * us_gallons_per_liter / _
            uk_gallons_per_liter
    ElseIf (optKilometers.Value) Then
        gallons = gallons * us_gallons_per_liter
    End If

    ' Calculate performance values.
    us_mpg = miles / gallons
    us_gal_per_100_miles = 100 / us_mpg

    ' US values.
    txtUsMpg.Text = Format$(us_mpg, "0.00")
    txtUsGalPer100Miles.Text = Format$(us_gal_per_100_miles, _
        "0.00")

    ' UK values.
    uk_gals = gallons * uk_gallons_per_liter / _
        us_gallons_per_liter
    uk_mpg = miles / uk_gals
    uk_gal_per_100_miles = 100 / uk_mpg
    txtUkMpg.Text = Format$(uk_mpg, "0.00")
    txtUkGalPer100Miles.Text = Format$(uk_gal_per_100_miles, _
        "0.00")

    ' Metric values.
    liters = gallons / us_gallons_per_liter
    kilometers = miles / miles_per_km
    km_per_liter = kilometers / liters
    l_per_100_km = 100 / km_per_liter
    txtKpl.Text = Format$(km_per_liter, "0.00")
    txtLitersPer100km.Text = Format$(l_per_100_km, "0.00")
End Sub
 
The code starts by defining some conversion factors to translate between different units of measurement.

Next the program gets the user's inputs and converts them into miles and US gallons. A handy trick for working with multiple units is to first convert the inputs into some known unit. In this example I've chosen miles and US gallons, but you could use whatever units you prefer.

The code then calculates miles per gallons and US gallons per 100 miles, and displays the result. After that the code simply converts those value into other units of measure and displays their results.

 
 
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated