Skip to main content

How to Convert Kilometers to Miles and Vice Versa in Batch Script

Unit conversion is a practical requirement for scripts that process geographic data, logistics reports, or hardware specifications from international sources. Converting between Kilometers and Miles requires a simple multiplication by a conversion factor. The challenge in Batch is handling this without decimal support, which requires a "Scaled Integer" approach.

In this guide, we will demonstrate how to perform both conversions with reasonable integer precision.

The Conversion Factors​

  • Kilometers to Miles: Miles = Kilometers × 0.621371
  • Miles to Kilometers: Kilometers = Miles × 1.60934

Since Batch cannot use decimals, we multiply by a scaled integer and then divide:

  • Miles ≈ Kilometers × 621 / 1000
  • Kilometers ≈ Miles × 1609 / 1000

Implementation Script​

@echo off
setlocal enabledelayedexpansion

:menu
cls
echo --- DISTANCE CONVERTER ---
echo 1. Kilometers to Miles
echo 2. Miles to Kilometers
echo 3. Exit
echo.
set /p "choice=Select an option: "

if "!choice!"=="1" goto :km2mi
if "!choice!"=="2" goto :mi2km
if "!choice!"=="3" exit /b
goto :menu

:km2mi
set /p "val=Enter Kilometers: "

:: Calculate whole and decimal parts separately
set /a "whole=val * 621371 / 1000000"
set /a "frac=(val * 621371 %% 1000000) / 10000"
if !frac! LSS 10 set "frac=0!frac!"

echo.
echo !val! km is approximately !whole!.!frac! miles.
pause
goto :menu

:mi2km
set /p "val=Enter Miles: "

:: Calculate whole and decimal parts separately
set /a "whole=val * 160934 / 100000"
set /a "frac=(val * 160934 %% 100000) / 1000"
if !frac! LSS 10 set "frac=0!frac!"

echo.
echo !val! miles is approximately !whole!.!frac! km.
pause
goto :menu
How the Scaled Integer Approach Works

Since Batch only supports integer arithmetic, we simulate decimal multiplication by using a larger numerator and dividing by the corresponding power of 10. For example, instead of val × 0.621371, we compute val × 621371 / 1000000. The whole part comes from integer division, and the fractional part comes from the modulo remainder, scaled down to two decimal places.

Overflow for Large Inputs

The intermediate product val * 621371 overflows Batch's 32-bit signed integer limit (2,147,483,647) when val exceeds approximately 3,455. For larger values, reduce the scale factor precision (e.g., use val * 6214 / 10000) or use the PowerShell bridge below.

Why Convert Distance Units in Batch?​

  1. International Reporting: If your organization has offices in both the US (Miles) and Europe (Kilometers), converting distances in logistics or delivery reports ensures consistency.
  2. Network Cabling: Converting between metric and imperial measurements for planning cable runs in international data centers.
  3. Geographic Calculations: Scripts that process GPS coordinate deltas often need to express distances in both units.

Important Limitations​

Integer Truncation

All Batch division truncates toward zero, there is no rounding. The result of 5 km × 621371 / 1000000 is 3 (the true value is 3.107). The two-decimal-place output mitigates this, but the last digit may still differ from a properly rounded result.

Adjusting Precision vs. Range

There is a tradeoff between decimal precision and the maximum input value:

Scale FactorFormulaMax InputPrecision
× 621 / 1000Low precision~3,457,300Whole number
× 6214 / 10000Medium~345,500~1 decimal
× 621371 / 1000000High precision~3,455~2 decimals

Choose the scale factor that best fits your expected input range.

PowerShell for Precision​

@echo off
setlocal

set /p "km=Enter Kilometers: "

for /f "usebackq delims=" %%R in (`
powershell -NoProfile -Command "[math]::Round(%km% * 0.621371, 2)"
`) do set "miles=%%R"

echo %km% km = %miles% miles
pause
Why PowerShell?

PowerShell supports floating-point arithmetic natively and the [math]::Round() method provides precise control over decimal places. Use it when your inputs may be large or when exact decimal output is required.

Conclusion​

Converting between kilometers and miles is a simple but practical tool for any script handling international data. While Batch's integer-only math introduces small rounding errors, the scaled-integer approach provides a reliable approximation. For applications requiring high precision, the PowerShell bridge fills the gap seamlessly.