January 30th, 2015

PowerTip: Use PowerShell to Round to Specific Decimal Place

Doctor Scripto
Scripter

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)

111

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

111.22

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

111.223

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.

Feedback