Questions

Forum Navigation
Please to create posts and topics.

Powershell - How to round a number to a specific decimal place?

How can I round a number, not integer, to a specific decimal place, in Powershell?

In Powershell we can use the Round static method from the System.Math class. By default, it does not display decimal places (if used without the relative parameter), but you can easily change that behavior. See the following examples to see how to achieve this:

PS C:\> $a = 111.2226

PS C:\> [math]::Round($a)
111

PS C:\> [math]::Round($a,2)
111.22

PS C:\> [math]::Round($a,3)
111.223