C# Convert Miles to Kilometers

Abbreviation for kilometer (km)

You want to convert miles to kilometers, and kilometers to miles. The USA has standardized on miles, but most other countries use the superior metric system. It is important for you to maintain compatibility with many cultures.

Required input and output for distance conversion

Input:  200 m
	321.9 km
	500 km
	310.7 m

Output: 321.9 km
	200 m
	310.7 m
	500 km

Convert distances

Here we look at some useful conversion methods for this problem. The solution consists of one static class and two methods in it. These methods are effective in converting from miles to kilometers and back again. They are in the public domain and are tested.

This C# example program does a numeric conversion from miles to kilometers.

Program that converts miles and kilometers [C#]

using System;

class Program
{
    static void Main()
    {
	//
	// Convert miles to kilometers.
	//
	double miles1 = 200;
	double kilometers1 = ConvertDistance.ConvertMilesToKilometers(200);
	Console.WriteLine("{0} = {1}", miles1, kilometers1);

	//
	// Convert kilometers to miles.
	//
	double kilometers2 = 321.9;
	double miles2 = ConvertDistance.ConvertKilometersToMiles(321.9);
	Console.WriteLine("{0} = {1}", kilometers2, miles2);

	//
	// Convert kilometers to miles (again).
	//
	double kilometers3 = 500;
	double miles3 = ConvertDistance.ConvertKilometersToMiles(500);
	Console.WriteLine("{0} = {1}", kilometers3, miles3);

	//
	// Convert miles to kilometers (again).
	//
	double miles4 = 310.7;
	double kilometers4 = ConvertDistance.ConvertMilesToKilometers(310.7);
	Console.WriteLine("{0} = {1}", miles4, kilometers4);
    }
}

public static class ConvertDistance
{
    public static double ConvertMilesToKilometers(double miles)
    {
	//
	// Multiply by this constant and return the result.
	//
	return miles * 1.609344;
    }

    public static double ConvertKilometersToMiles(double kilometers)
    {
	//
	// Multiply by this constant.
	//
	return kilometers * 0.621371192;
    }
}

Output

200 = 321.8688
321.9 = 200.0193867048
500 = 310.685596
310.7 = 500.0231808
Double keyword

Description. Both methods use double. Double stores numbers that are larger and have decimal places. It consumes more memory and is slower to process. The doubles here are float64 types, meaning they are twice as large as an int.

Double Type

Constants used. Each method above uses a constant for the conversion. For regular stuff, these are fine. The compiler will treat these constants internally as float64 values. These are in a static class, which doesn't store state. Static classes are just a syntactic feature of the C# 2.0 language, but they help clarify the code.

Static Class

Intermediate language

.NET Framework information

As I noted, the double type here is actually termed float64, meaning a 64-bit number. This snippet shows the ConvertKilometersToMiles method. You can see it uses the mul opcode to multiply. The ldrc.r8 opcode pushes the supplied value (the 0.621371192) onto the stack as a float. This helps us understand what is happening under the hood.

Method implementation [IL]

.method public hidebysig static float64 ConvertKilometersToMiles(float64 k)
    cil managed
{
    .maxstack 8
    L_0000: ldarg.0
    L_0001: ldc.r8 0.621371192
    L_000a: mul
    L_000b: ret
}

Summary

The C# programming language

We looked at how you can convert figures in miles to kilometers in the C# programming language. It is interesting the extent of the loss of precision. In chemistry, there is a concept of significant digits, and that concept certainly applies here as well. We saw how the methods are converted into MSIL, and also saw how we can use a static class to store static methods more logically in the C# language.

Cast Examples
.NET