PowerTip: Round a Number with PowerShell

The Scripting Guys

Dr Scripto

Summary: Use Windows PowerShell to round numbers to a specific decimal place.

Hey, Scripting Guy! Question How can I use Windows PowerShell to round a number to a specific number of decimal places, and continue to
           have a number instead of converting it to a string.

Hey, Scripting Guy! Answer Use the Round static method from the System.Math class. By default, it does not display decimal places, but
           you can easily change that behavior. The following code illustrates this procedure:

PS C:\> $a = 111.2226

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


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


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


The Scripting Guys
Dr Scripto

Follow Dr Scripto   

No Comments.