Skip to main content

How to Do Floating-Point Math in Batch Script (via PowerShell)

A major, well-known limitation of the Windows Batch command processor is its inability to handle floating-point numbers (numbers with decimals). The built-in SET /A command is strictly limited to 32-bit integer arithmetic. Any division that would result in a fraction is truncated, and you cannot store or operate on numbers like 3.14 or 99.5.

This makes it impossible to perform many common calculations, such as precise financial math, scientific computations, or even simple division. This guide will teach you the modern, standard, and only recommended method for performing floating-point math in a batch script: by delegating the calculation to the powerful PowerShell engine that is built into all modern versions of Windows.

The Core Problem: Batch is Integer-Only

The SET /A command, which is the only native way to perform math in batch, simply cannot see decimal points.

Example of script with error:

@ECHO OFF
REM This will FAIL.
SET /A "Result=10 / 4"
ECHO 10 divided by 4 is: %Result%

REM This will also FAIL with a syntax error.
SET /A "Result=99.5 * 2"

Output:

10 divided by 4 is: 2
Invalid number. Numeric constants are either decimal (17),
hexadecimal (0x11), or octal (021).

The first calculation, 10 / 4, results in 2 because the 0.5 part is completely discarded (truncated). The second calculation fails because 99.5 is not a valid integer.

The Solution: Delegating to PowerShell

The correct way to solve this is to pass the calculation to a tool that can handle floating-point math. The PowerShell engine is the perfect choice because it is built into Windows and has a powerful, modern math parser. We can construct a PowerShell command as a string and execute it from our batch script.

The Core Method: Calling PowerShell with FOR /F

The standard pattern for running a command and capturing its output into a variable is to use a FOR /F loop. This works perfectly for our PowerShell calculations.

Syntax: FOR /F %%V IN ('powershell -Command "your-calculation-here"') DO SET "Result=%%V"

  • powershell -Command "...": Executes the PowerShell code.
  • your-calculation-here: The mathematical expression you want to evaluate (e.g., 10 / 4). PowerShell will calculate it and print the result.
  • FOR /F ... SET "Result=%%V": This captures the text that PowerShell printed and stores it in the Result batch variable.

Basic Examples of Floating-Point Calculations

Simple Division

For example, let's correctly calculate 10 / 4.

@ECHO OFF
SET "Result="
FOR /F %%N IN ('powershell -Command "10 / 4"') DO (
SET "Result=%%N"
)
ECHO The result of 10 / 4 is: %Result%

Output:

The result of 10 / 4 is: 2.5

Multiplication

For example, now let's multiply by a decimal.

@ECHO OFF
SET "Result="
FOR /F %%N IN ('powershell -Command "12.75 * 5"') DO (
SET "Result=%%N"
)
ECHO The result of 12.75 * 5 is: %Result%

Output:

The result of 12.75 * 5 is: 63.75

More Complex Formulas

You can pass entire formulas to PowerShell, and it will respect the correct order of operations. You can even use variables from your batch script inside the PowerShell command.

@ECHO OFF
SET "Length=5.5"
SET "Width=10.2"
SET "Area="

FOR /F %%A IN ('powershell -Command "%Length% * %Width%"') DO (
SET "Area=%%A"
)

ECHO The area of a %Length% x %Width% rectangle is: %Area%

Output:

The area of a 5.5 x 10.2 rectangle is: 56.1

Common Pitfalls and How to Solve Them

  • Locale and Decimal Separators: The biggest potential issue is that PowerShell, by default, uses the system's regional settings. In the US, the decimal separator is a period (.), but in Germany, it's a comma (,). A calculation like 5.5 * 10.2 might fail on a German system.

    • Solution: For the most robust scripts, you can force a specific culture (like US English) within the PowerShell command or use the .NET [double]::Parse() method, but for most cases, the simple method works.
  • Passing Unsafe Variables: If the batch variables you are passing to PowerShell contain characters that are special to PowerShell (like $, (, or )), it could break the command.

    • Solution: Ensure that the variables you pass only contain numbers and basic operators.

Practical Example: Converting Temperature from Fahrenheit to Celsius

This script takes a temperature in Fahrenheit, which includes a decimal, and correctly calculates the Celsius equivalent using the formula (F - 32) * 5 / 9. This would be impossible with SET /A.

@ECHO OFF
SETLOCAL

SET "Fahrenheit=98.6"
SET "Celsius="

ECHO --- Temperature Converter ---
ECHO.
ECHO Fahrenheit temperature: %Fahrenheit%

REM --- The PowerShell Calculation ---
SET "PowerShellCommand=(%Fahrenheit% - 32) * 5 / 9"
FOR /F %%C IN ('powershell -Command "%PowerShellCommand%"') DO (
SET "Celsius=%%C"
)

ECHO Celsius temperature: %Celsius%
ECHO.

REM --- Formatting the output to one decimal place ---
SET "FormattedCelsius="
FOR /F %%C IN ('powershell -Command "{0:F1}" -f %Celsius%') DO (
SET "FormattedCelsius=%%C"
)
ECHO Formatted to one decimal place: %FormattedCelsius%

ENDLOCAL

Output:

--- Temperature Converter ---

Fahrenheit temperature: 98.6
Celsius temperature: 37

Formatted to one decimal place: 37.0

Conclusion

While native batch scripting is limited to integer math, this limitation is easily and completely overcome by delegating calculations to the built-in PowerShell engine.

  • Batch SET /A is for integers only.
  • The PowerShell one-liner is the standard and only recommended method for any floating-point (decimal) calculations.
  • The core pattern is to use FOR /F to execute the powershell -Command "..." and capture its output into a batch variable.

By leveraging this hybrid technique, you can perform any mathematical operation you need, combining the simplicity of a batch script with the full power of the PowerShell math engine.