Rounding a number is a mathematical process of replacing a number with another which is approximately the same but has fewer digits. PowerShell has various .NET framework classes or methods that can be used to round up or round down the numbers. Rounding up will round the number to the closest up number. While rounding down will round the number nearest down number.
This post will discuss PowerShell math functions.
PowerShell Round Up Function
These methods will be observed in this article for rounding a number in PowerShell:
Method 1: Rounding a General in PowerShell
In PowerShell, use the “[Math]::Round()” method to round a number. This method takes two arguments. First, the argument will take the decimal which will be rounded by the function. The second one will be to which the decimal will be rounded.
Example
This example will round a number using the “[Math]::Round()” method or function:
[math]::Round($Number)
It can be observed that the value “11.5” has been successfully rounded to “12”.
Method 2: Rounding Up a Number in PowerShell
The “[Math]::ceiling()” cmdlet in PowerShell is used to round up a function or number. It rounds the function or number to the nearest whole number.
Example
This example will round up a number to the nearest whole number using the “[Math]::ceiling()” method:
[math]::ceiling($Number)
Method 3: Rounding Down a Number in PowerShell
The “[Math]::Floor()” method is used in PowerShell to round down a decimal to its nearest small integer number.
Example
This instance will round down a number in PowerShell:
[math]::Floor($Number)
As a result, the specified method will round the number “11.5” to “11” (as the smallest nearest integer).
Conclusion
PowerShell has various math functions that can be used to round, round up or round down decimal numbers to integers. These methods include “[math]::Round()”, “[math]::ceiling()”, or “[math]::Floor()”. This article has discussed several PowerShell math functions to round up a number.