January 29th, 2015

PowerTip: Express Percentage in PowerShell

Doctor Scripto
Scripter

Summary: Learn an easy method to express a percentage in Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to express a two-decimal place percentage? For example, I have multiple
           calculations in a Windows PowerShell script, and I am tired of always dividing two numbers, multiplying
           by a hundred, and trimming the results to two decimal places.

Hey, Scripting Guy! Answer Use the special “P” format specifier as an overload to the ToString method, for example:

PS C:\> (5/21).tostring("P")

23.81 %

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.