HomeSearch

VB.NET Convert Miles to Kilometers

This VB.NET program performs a numeric conversion from miles to kilometers.
Convert miles, kilometers. It is possible to convert figures in miles to kilometers. The reverse from kilometers to miles is also available. We use a simple Double multiplication. We look at these conversion Functions in the VB.NET programming language.Double
Example. 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.

Note: This provides a close approximation of your distance in the opposite unit.

VB.NET program that converts miles and kilometers 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.WriteLine. Let us 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.

And: The {1:0.0} marker is where the third argument is inserted. The 0.0 part specifies one digit after the decimal place is displayed.

Console
Summary. Numeric conversions, such as from miles to kilometers, are easily done in the VB.NET 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.

C# Convert Miles
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls