Converting VBScript's Round Function

Definition: Returns a number rounded to a specified number of decimal places.

Round

When working with system administration scripts (and performing chores like returning free disk space on a drive) you’ll often get back numbers similar to this: 45.987654321. More often than not you don’t really need all those decimal places; instead, you’d just as soon round the value up (or down).

OK; in Windows PowerShell all you have to do is use the System.Math class and the Round method. This command rounds the number 45.987654321 to two decimal places (notice the value 2 passed as the second parameter to the method). The resulting value is then stored in the variable $a:

$a = [math]::round(45.987654321, 2)

When you run this command and then echo back the value of $a you should get the following:

45.99

Return to the VBScript to Windows PowerShell home page