PowerShell Round to Whole Number

PowerShell round to whole number

Using [Math]::Round Method

Use the [Math]::Round to round number to whole number in PowerShell.

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:

  1. The number that you want to round.
  2. 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:

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.

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.

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.

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.

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:

The Ceiling function will always round up a number to the next highest whole number.

Using [Math]::Floor method:

Use the [Math]::Floor method to round down a number to the whole number in PowerShell.

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:

The [Math]::Floor method will always round a number down to the nearest whole number.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *