VB.NET Convert Miles to Kilometers

Abbreviation for kilometer (km)

It is possible to convert figures in miles to kilometers, and from kilometers to miles, with a simple Double multiplication. Here, we look at these conversion Functions in the VB.NET programming language.

Example

Note

We introduce two Functions: ConvertMilesToKilometers, and ConvertKilometersToMiles. In each Function body, the argument is multiplied by a constant value and that result is returned from the Function. This provides a very close approximation of your distance in the opposite unit.

This VB program performs a numeric conversion from miles to kilometers.

Program that converts miles and kilometers [VB.NET]

Module Module1

    Function ConvertMilesToKilometers(ByVal miles As Double) As Double
	Return miles * 1.609344
    End Function

    Function ConvertKilometersToMiles(ByVal kilometers As Double) As Double
	Return kilometers * 0.621371192
    End Function

    Sub Main()
	Dim milesValues() As Double = {200.0, 310.7}
	For Each value As Double In milesValues
	    Console.WriteLine("{0} miles -> {1:0.0} km", value, ConvertMilesToKilometers(value))
	Next

	Dim kilometersValues() As Double = {321.9, 500.0}
	For Each value As Double In kilometersValues
	    Console.WriteLine("{0} km -> {1:0.0} miles", value, ConvertKilometersToMiles(value))
	Next
    End Sub
End Module

Output

200 miles -> 321.9 km
310.7 miles -> 500.0 km
321.9 km -> 200.0 miles
500 km -> 310.7 miles
Console screenshot

Console.WriteLine. Let's note the Console.WriteLine function calls. They use a format string: the {0} is a substitution marker and the second argument to WriteLine is inserted there. The {1:0.0} marker is where the third argument is inserted. The 0.0 part specifies that only one digit after the decimal place is to be displayed.

Console.Write Program

Summary

The VB.NET programming language

Numeric conversions, such as from miles to kilometers, are easily done in the VB.NET programming language. Typically, the Double type is good for these, as it retains more significant digits in a multiplication expression than does the Integer type. Finally, for verification that these conversions are correct, please see the alternative version of this article.

Convert Miles to Kilometers VB.NET Tutorials
.NET