Table of Contents
Using [Math]::Round
Method
Use the [Math]::Round
to round number to whole number in PowerShell.
1 2 3 4 5 6 |
$decimalNum = 3.14159 $roundedNum = [Math]::Round($decimalNum, 0) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 3.14159 Rounded number: 3 |
We can observe the [Math]::Round
method rounds a decimal number to the nearest whole number. In PowerShell, the [Math]::Round
method takes two arguments:
- The number that you want to round.
- The number of decimal places to round to.
Here, the variable $decimalNum
is given to the [Math]::Round
function as the first argument, representing the decimal number we wanted to round off. The second argument is set to 0
to round to the nearest whole number. This tells the Round
method to round to the nearest integer value. Finally, the Write-Host
cmdlets print the original and rounded numbers on the console.
Have a look at another example to get a better understanding:
1 2 3 4 5 6 |
$decimalNum = 10/6 $roundedNum = [Math]::Round($decimalNum, 0) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 1.66666666666667 Rounded number: 2 |
In this example, the $number
variable was initialized with 10/6
, which results in a value of 1.66666666666667
after division. Then, the [Math]::Round
method is utilized to round the value to the nearest whole number.
This method always rounds up when the number is halfway between two possible results.
Using int
Typecast
Use the int
typecast to round number to whole number in PowerShell.
1 2 3 4 5 6 |
$decimalNum = 4.37 $roundedNum = [int]$decimalNum Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 4.37 Rounded number: 4 |
In this example, the int
typecast converts a decimal number 4.37
to an integer 4
.
When rounding a number, if the decimal portion is less than 0.5
, it will be rounded down to the nearest whole number. On the other hand, if the decimal part is greater than or equal to 0.5
, it will be rounded up to the nearest whole number. Let’s see an example to understand it.
1 2 3 4 5 6 |
$decimalNum = 4.67 $roundedNum = [int]$decimalNum Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 4.67 Rounded number: 5 |
In the given example, the number being typecast has a decimal portion of 0.6
, which is greater than 0.5
, so the int
typecast rounds it up to the nearest whole number of 5
.
Using [Math]::Truncate
Method:
Use the [Math]::Truncate
method to round number to whole number in PowerShell.
1 2 3 4 5 6 |
$decimalNum = 4.14 $roundedNum = [Math]::Truncate($decimalNum) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 4.14 Rounded number: 4 |
In the above example, the Truncate()
method in the [Math]
class rounded a number to the nearest whole number. This method accepts a variable $decimalNum
as an input, the number we want to truncate or round off. It removed decimal places from the number, effectively rounding down to the nearest whole number. So, in this case, the result is 4
.
Using [Math]::Ceiling
Method:
Use the [Math]::Ceiling
method to round up a number to the whole number in PowerShell.
1 2 3 4 5 6 |
$decimalNum = 4.56 $roundedNum = [Math]::Ceiling($decimalNum) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 4.56 Rounded number: 5 |
In the given PowerShell code snippet, the Ceiling()
method of the [Math]
class is utilized to round up the decimal number stored in the $decimalNum
variable to the nearest whole number.
Consider another example below:
1 2 3 4 5 6 |
$decimalNum = 4.67 $roundedNum = [Math]::Ceiling($decimalNum) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 4.67 Rounded number: 5 |
The Ceiling
function will always round up a number to the next highest whole number.
Further reading:
Using [Math]::Floor
method:
Use the [Math]::Floor
method to round down a number to the whole number in PowerShell.
1 2 3 4 5 6 |
$decimalNum = 1.14 $roundedNum = [Math]::Floor($decimalNum) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 1.14 Rounded number: 1 |
In this example, we first defined a variable $decimalNum
with a value of 1.14
. Then, the Floor
method from the [Math]
class rounded it down to the nearest whole number, 1
.
Have a look at another example below:
1 2 3 4 5 6 |
$decimalNum = 2.14 $roundedNum = [Math]::Floor($decimalNum) Write-Host "Original number: $decimalNum" Write-Host "Rounded number: $roundedNum" |
1 2 3 4 |
Original number: 2.14 Rounded number: 2 |
The [Math]::Floor
method will always round a number down to the nearest whole number.