Using formatting to export numbers
Numbers in their raw form are useful when you want the most exact calculation, but can become messy when presenting them to users. Because of this PowerShell uses standardized .NET
formatting rules to convert and present numbers in different contexts.
In this recipe, we will take a number and use PowerShell to present it in different number formats. This allows us to quickly see the differences between how PowerShell performs number formatting.
How to do it...
Carry out the following steps:
Start with a simple PowerShell script to present numbers using different formats:
$jenny = 1206867.5309 Write-Host "Original:`t`t`t" $jenny Write-Host "Whole Number:`t`t" ("{0:N0}" -f $jenny) Write-Host "3 decimal places:`t" ("{0:N3}" -f $jenny) Write-Host "Currency:`t`t`t" ("{0:C2}" -f $jenny) Write-Host "Percentage:`t`t`t" ("{0:P2}" -f $jenny) Write-Host "Scientific:`t`t`t" ("{0:E2}" -f $jenny) Write-Host "Fixed Point:`t`t" ("{0:F5}" -f $jenny) Write-Host "Decimal:`t`t`t" ("{0:D8}" -f [int]$jenny) Write-Host "HEX:`t`t`t`t" ("{0:X0}" -f [int]$jenny)
Execute the script and review the results:
How it works...
Because PowerShell is based on the .NET
framework, it automatically inherits its number formatting capabilities. In this script, we are creating a variable named $jenny
and assigning a number to it. Then, several number formatting strings are created (the string in the curly braces {}
) and the formatting is applied to $jenny
.
Note
In the code shown, we are using `t
(backtick + letter t) to make the output easier to read. This causes a tab to be added to the text, which then aligns the output on the right.
The backtick character on most keyboards is the key to the left of the number 1 and also contains the ~ character. In PowerShell this character is occasionally referred to as an Esc character and is used to pass special commands such as tabs and new lines.
The formatting strings are composed of three distinct elements, each with a unique role. Inside the curly braces, the first zero (before the colon) refers to the first variable being used. Since we are only using one variable, this number never changes. The letter after the colon defines the format type of number, percentage, decimal, and so on. And the final number defines how many decimal places to include in the results.
One area of special note is when we convert $jenny
to a decimal or hexadecimal number. You may have noticed the [int]
attribute before the variable. This attribute explicitly casts the variable as an integer prior to applying the formatting. This is necessary because decimal and hexadecimal numbers only work with integers by default. Attempting to pass a complex number like ours natively to these formatting commands will result in an error.
There's more...
In addition to the built in formatting strings shown previously, custom formatting strings can also be created and applied.
Write-Host "Static Size:`t`t" ("{0:0000000000.00}" -f $jenny) Write-Host "Literal String:`t`t" ("{0:000' Hello '000}" -f $jenny) Write-Host "Phone Number:`t`t" ("{0:# (###) ### - ####}" -f ($jenny*10000))
The first custom string creates a number that is composed of 10 digits, a decimal point, and two digits. If the number is not large enough to fill the formatting, zeros are prepended to it.
The second string creates a number with a literal string in the middle of it.
The third string multiplies the variable by 10,000 (to make it an 11 digit integer), and formats it into a phone number. The number is returned complete with county and area codes.
See also
More information on formatting in
.NET
Framework 4.5 is documented on MSDN at http://msdn.microsoft.com/en-us/library/26etazsy