How to Convert Between Celsius and Fahrenheit in Batch Script
Temperature conversion is a classic exercise in multi-step arithmetic. Whether you are building a weather monitoring script or a hardware thermal alert system, you often need to convert between Celsius (°C) and Fahrenheit (°F). Because Batch math only works with integers, we have to be careful with the order of operations to maintain as much precision as possible.
In this guide, we will demonstrate how to perform these conversions using the set /a command.
The Formulas
- Celsius to Fahrenheit: $F = (C \times 9 / 5) + 32$
- Fahrenheit to Celsius: $C = (F - 32) \times 5 / 9$
Implementation Script
@echo off
setlocal
:menu
cls
echo --- TEMPERATURE CONVERTER ---
echo 1. Celsius to Fahrenheit
echo 2. Fahrenheit to Celsius
echo 3. Exit
echo.
set /p "choice=Select an option: "
if "%choice%"=="1" goto c2f
if "%choice%"=="2" goto f2c
if "%choice%"=="3" exit /b
goto menu
:c2f
set /p "temp=Enter degrees Celsius: "
:: Multiplication before division is critical for integer precision
set /a "result=(temp * 9 / 5) + 32"
echo.
echo %temp%C is approximately %result%F
pause
goto menu
:f2c
set /p "temp=Enter degrees Fahrenheit: "
:: Subtract 32 first, then multiply, then divide
set /a "result=(temp - 32) * 5 / 9"
echo.
echo %temp%F is approximately %result%C
pause
goto menu
Label references in goto and if ... goto statements should not include the colon prefix. The colon is only used when defining the label (:menu), not when jumping to it (goto menu).
Why Mathematical Order Matters in Batch
Since Batch throws away any decimal remainder (integer truncation), the Order of Operations is vital:
- Multiply first, Divide later: If you do
temp / 5 * 9, andtempis 4, Batch calculates4 / 5 = 0, then0 * 9 = 0. This is wrong. - Parentheses: Always use parentheses
( )to ensure the addition/subtraction happens at the correct time (e.g., subtracting 32 before multiplying by 5).
Important Limitations
- No Decimals: A precise conversion might result in
37.77, but Batch will display37. - Accuracy Gap: Because of the integer truncation, small errors will accumulate. For everyday use (is the CPU too hot?), this is usually acceptable. For scientific lab work, it is not.
- 32-Bit Limit: While not an issue for typical weather (which stays between -100 and 200), huge numbers could technically overflow.
High Precision Alternative (PowerShell)
For professional environmental monitoring with decimal precision, use this PowerShell one-liner inside your Batch file:
@echo off
setlocal
set "c=25"
for /f "usebackq delims=" %%r in (`powershell -NoProfile -Command "([double]%c% * 9 / 5) + 32"`) do set "result=%%r"
echo %c%C is %result%F
pause
endlocal
Wrapping the powershell call in a for /f loop lets you capture the output into a Batch variable for further use in your script. The -NoProfile flag speeds up execution by skipping the user's PowerShell profile.
Conclusion
Converting temperatures in Batch is a practical way to manage hardware thresholds and environmental logs. By understanding the integer-math constraints and prioritizing multiplication over division, you can build reliable converters that support your system monitoring needs. This ensures your thermal alerts and weather trackers are functionally accurate within the standard Windows command-line environment.