[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Compare fuel efficiency in different units in C#

[Compare fuel efficiency in different units in C#]

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 even compare values in miles per gallon across different countries.

This example calculates fuel efficiency in three systems: US miles per gallon, UK miles per gallon, and liters per 100 kilometers. The following code performs the calculations.

// Conversion factors. private const float miles_per_km = 0.621371192f; private const float us_gallons_per_liter = 0.264172052637296f; private const float uk_gallons_per_liter = 0.2199692483f; // Calculate the mileage statistics. private void btnCalculate_Click(object sender, EventArgs e) { // Get the inputs. float miles = float.Parse(txtDistance.Text); float gallons = float.Parse(txtFuel.Text); // Convert to miles and US gallons. if (radKilometers.Checked) miles *= miles_per_km; if (radUkGallons.Checked) { gallons *= us_gallons_per_liter / uk_gallons_per_liter; } else if (radKilometers.Checked) { gallons *= us_gallons_per_liter; } // Calculate performance values. float us_mpg = miles / gallons; float us_gal_per_100_miles = 100 / us_mpg; // US values. txtUsMpg.Text = us_mpg.ToString("0.00"); txtUsGalPer100Miles.Text = us_gal_per_100_miles.ToString("0.00"); // UK values. float uk_gals = gallons * uk_gallons_per_liter / us_gallons_per_liter; float uk_mpg = miles / uk_gals; float uk_gal_per_100_miles = 100 / uk_mpg; txtUkMpg.Text = uk_mpg.ToString("0.00"); txtUkGalPer100Miles.Text = uk_gal_per_100_miles.ToString("0.00"); // Metric values. float liters = gallons / us_gallons_per_liter; float kilometers = miles / miles_per_km; float km_per_liter = kilometers / liters; float l_per_100_km = 100 / km_per_liter; txtKpl.Text = km_per_liter.ToString("0.00"); txtLiterPer100km.Text = l_per_100_km.ToString("0.00"); }

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 gallon and US gallons per 100 miles, and displays the result. After that the code simply converts those values into other units of measure and displays their results.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.