Monthly Archive: December 2012

Dec 06 2012

Powershell and the Customisation of Numerical Results

In the event of running a powershell script and you are returned with values with an unreadable number of decimal places or in the incorrect format (not Currency, Percentage or Hex etc), you can force powershell to output the correct format of this number using some simple “conversion’ techniques.

Using the “{0:XX}” -f pre cursor to convert the chosen variable, you can tell powershell that you want the variable output in the chosen format with the selected number of decimals

If we set the variable $var = 123.1234 and then use the following precursors to decide wether we want to displayu a Number with fixed decimals (N), Currency (C) or Percentage (P) we will get the output shown:

$var =123.1234




In the event of wanting to use the X (Hex) or D (Decimal) convertors you will need to ensure that you variable is a whole number.

$var = 123