Home
C#
Convert Miles to Kilometers
Dot Net Perls
Convert miles, kilometers. A figure in miles can be converted to one in kilometers with a C# program. The United States has standardized on miles.
Conversion notes. Most other countries use the superior metric system. It is important to maintain compatibility with many cultures.
Required input, output. Consider the figure 200 miles. This is equivalent to 321.9 kilometers. At a minimum, our method should handle these cases.
Input: 200 m 321.9 km 500 km 310.7 m Output: 321.9 km 200 m 310.7 m 500 km
Example code. Here we look at some useful conversion methods for this problem. These methods are effective in converting from miles to kilometers and back again.
Detail Double stores numbers that are larger and have decimal places. It consumes more memory and is slower to process.
double
Detail Each method here uses a constant for the conversion. For regular stuff, these are fine.
Detail The compiler will treat these constants internally as float 64 values. These are in a static class, which doesn't store state.
Tip Static classes are just a syntactic feature of the C# language, but they help clarify the code.
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; } }
200 = 321.8688 321.9 = 200.0193867048 500 = 310.685596 310.7 = 500.0231808
A discussion. The double type here is actually termed float64, meaning a 64-bit number. This snippet shows ConvertKilometersToMiles. It uses the mul opcode to multiply.
Info The ldrc.r8 opcode pushes the supplied value (the 0.621371192) onto the stack as a float.
.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 }
A summary. Figures in miles can be converted to km—this can be helpful in many programs that act upon distances. We used a static class to logically store static methods.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.