PowerTip: Express Percentage in PowerShell

Doctor Scripto

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 %

0 comments

Discussion is closed.

Feedback usabilla icon