HOWTO · PowerShell
How to Round Numbers in PowerShell
Learn when to use PowerShell Math methods to truncate, round down or up, and round numbers to a whole number or chosen decimal places.
On this page
PowerShell provides several ways to change a number’s precision, and the right choice depends on the result you need. Use [Math]::Truncate to remove the fractional part toward zero, [Math]::Floor or [Math]::Ceiling for directed rounding, and [Math]::Round for nearest-value rounding. To round to a chosen number of decimal places or control midpoint ties, use the appropriate [Math]::Round overload.
Choose the rounding behavior that matches your task
These operations do different jobs. Truncation removes the fraction and moves toward zero. Floor moves toward negative infinity, while ceiling moves toward positive infinity. Nearest-value rounding uses [Math]::Round; its default midpoint rule is to-even. Choose the operation before formatting or displaying a value, because formatting changes how a number is shown but does not change the underlying numeric value.
Run this example:
$value = 12.34
[Math]::Round($value, 1)
12.3
Truncate toward zero with [Math]::Truncate
[Math]::Truncate discards the fractional part and moves toward zero. For a positive input this resembles rounding down, but for a negative input it moves upward on the number line. For example, truncating -4.8 gives -4, whereas flooring it gives -5. Use truncation when you need to remove fractional digits without always moving toward negative infinity.
$positive = [Math]::Truncate(4.8)
$negative = [Math]::Truncate(-4.8)
"$positive, $negative"
4, -4
Round down with [Math]::Floor and up with [Math]::Ceiling
[Math]::Floor returns the greatest whole number less than or equal to the input, so it moves toward negative infinity. [Math]::Ceiling returns the least whole number greater than or equal to the input, so it moves toward positive infinity. The direction is about the number line, not the size of the absolute value: for a negative number, floor can produce a more negative result, while ceiling can produce a less negative one.
$value = -4.2
$down = [Math]::Floor($value)
$up = [Math]::Ceiling($value)
"$down, $up"
-5, -4
Round to a whole number with [Math]::Round
[Math]::Round(value) returns the nearest whole number. When the value is exactly halfway between two integers, the default MidpointRounding behavior is to-even: the result is the even neighboring integer. This avoids a systematic upward bias across repeated midpoint rounding. The ties 62.5 and 63.5 therefore go to different neighbors. This is not the same as always rounding a half upward.
$firstTie = [Math]::Round(62.5)
$secondTie = [Math]::Round(63.5)
"$firstTie, $secondTie"
62, 64
Round to a chosen number of decimal places
Use [Math]::Round(value, digits) to round to a specified number of decimal places. The digits argument is a count of decimal places: 0 requests a whole number, 1 requests one decimal place, and 2 requests two. The result remains a numeric value; it is not a fixed-width display string, so trailing zeros may not appear. For prices or other fixed-scale values, format the result separately when displaying it, and use an appropriate numeric type and rounding policy for calculations.
$value = 63.827439
$whole = [Math]::Round($value, 0)
$onePlace = [Math]::Round($value, 1)
$twoPlaces = [Math]::Round($value, 2)
"$whole; $onePlace; $twoPlaces"
64; 63.8; 63.83
Choose midpoint behavior explicitly
When the default to-even rule is not the policy you need, pass a MidpointRounding value to [Math]::Round. MidpointRounding.AwayFromZero sends an exact midpoint away from zero: positive ties increase and negative ties decrease. Select this rule explicitly when your requirements call for half-away-from-zero behavior. Other midpoint rules are available in .NET, so check the required policy rather than assuming that “round” always means one universal tie rule.
$positiveTie = [Math]::Round(2.5, [MidpointRounding]::AwayFromZero)
$negativeTie = [Math]::Round(-2.5, [MidpointRounding]::AwayFromZero)
"$positiveTie, $negativeTie"
3, -3