# Rounding Numbers in PowerShell

- Truncating Numbers in PowerShell
- Rounding Down to Whole Numbers in PowerShell
- Rounding Up to Whole Numbers in PowerShell
- Rounding in General in PowerShell

Rounding is the mathematical process of replacing a number with another number of approximately the same value but having fewer digits. In PowerShell, we can quickly achieve this by using one of the classes provided by the .NET Framework.

This article will discuss how to round numbers in PowerShell using multiple functions of the .NET framework Math class.

## Truncating Numbers in PowerShell

Truncating in PowerShell may not precisely be a function that rounds a number but is still considered to calculate an integral part of the number. In other words, the method drops the decimal part of the integer, rounding it down to the nearest whole number.

To truncate (or crop) a number, we will be using the math class functions called `[Math]::Truncate`

. It accepts an integer with a `Double`

data type, the decimal numbers.

Example Code:

```
$decimalNum = 63.82
[Math]::Truncate($decimalNum)
```

Output:

```
63
```

## Rounding Down to Whole Numbers in PowerShell

PowerShell has a math class function called `[Math]: Floor`

, which crops the integer’s decimal and truly rounds it down mathematically to a whole number in the backend.

Example Code:

```
$decimalNum = 63.82
[Math]::Floor($decimalNum)
```

Output:

```
63
```

## Rounding Up to Whole Numbers in PowerShell

PowerShell has a math class function called `[Math]::Ceiling`

, which rounds up the decimal number to the nearest whole number.

Example Code:

```
$decimalNum = 63.82
[Math]::Ceiling($decimalNum)
```

Output:

```
64
```

## Rounding in General in PowerShell

We can also use the official `[Math]::Round`

function to have more flexibility than rounding to a whole number.

The `[Math]::Round`

function accepts two arguments. The first one would be the decimal that the function will round, and the second one would be to which decimal place the integer will be rounded (`0`

for the whole number, `1`

for one decimal number, `2`

for two decimal number, and so on).

Example Code:

```
$decimalNum = 63.827439
[Math]::Round($decimalNum,0)
[Math]::Round($decimalNum,1)
[Math]::Round($decimalNum,2)
[Math]::Round($decimalNum,5)
```

Output:

```
64
63.8
63.83
63.82744
```

**Marion Paul Kenneth Mendoza**

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn