﻿ VB Helper: HowTo: Calculate mileage and fuel consumption in the United States, United Kingdom, and metric units in Visual Basic .NET     Home Search   What's New Index Books Links Q & A Newsletter Banners   Feedback Tip Jar       MSDN Visual Basic Community  Title Calculate mileage and fuel consumption in the United States, United Kingdom, and metric units in Visual Basic .NET This example shows how to calculate mileage and fuel consumption in the United States, United Kingdom, and metric units in Visual Basic .NET, mathematics, mileage, fuel consumption, metric, liters, miles per gallon, milometers per liter, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET Algorithms 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.

```' Conversions.
Private Const miles_per_km As Single = 0.6213712
Private Const us_gallons_per_liter As Single = 0.264172047
Private Const uk_gallons_per_liter As Single = 0.219969243

' Calculate the mileage statistics.
Private Sub btnCalculate_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnCalculate.Click
' Get the inputs.
Dim miles As Single = Single.Parse(txtDistance.Text)
Dim gallons As Single = Single.Parse(txtFuel.Text)

' Convert to miles and US gallons.
If (radKilometers.Checked) Then miles *= miles_per_km
gallons *= us_gallons_per_liter / _
uk_gallons_per_liter
gallons *= us_gallons_per_liter
End If

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

' US values.
txtUsMpg.Text = us_mpg.ToString("0.00")
txtUsGalPer100Miles.Text = _
us_gal_per_100_miles.ToString("0.00")

' UK values.
Dim uk_gals As Single = gallons * uk_gallons_per_liter / _
us_gallons_per_liter
Dim uk_mpg As Single = miles / uk_gals
Dim uk_gal_per_100_miles As Single = 100 / uk_mpg
txtUkMpg.Text = uk_mpg.ToString("0.00")
txtUkGalPer100Miles.Text = _
uk_gal_per_100_miles.ToString("0.00")

' Metric values.
Dim liters As Single = gallons / us_gallons_per_liter
Dim kilometers As Single = miles / miles_per_km
Dim km_per_liter As Single = kilometers / liters
Dim l_per_100_km As Single = 100 / km_per_liter
txtKpl.Text = km_per_liter.ToString("0.00")
txtLiterPer100km.Text = l_per_100_km.ToString("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 