Eggins.com
Powershell, .Net, Visual Studio, Team Foundation Server, Windows Communication Foundation

Powershell Number Formatting

The number formatting features available to Powershell though the dot net String.Format() function are a lot more exciting than the string formatting mentioned in my last post.

I will give some examples here, and you may look at the end of this post for more formatting specifiers.

To pad a number with leading zeros:
  "{0:00#}" -f 34
Produces
  034

To format a number with comma separators and two decimal places:
  "{0:0,0.00}" -f 443567443
Produces
  443,567,443.00

To format a number as currency using your region settings:
  "{0:c.00}" -f 443567443
Produces (for me)
  $443,567,443.00

To right-align a column of numbers, while specifying the numeric format:
  1,22,33.3333,44444.4 | %{"Free: {0,9:#,#.00} GB" -f $_}
Produces
  Free:      1.00 GB
  Free:     22.00 GB
  Free:     33.33 GB
  Free: 44,444.40 GB 

 

Numbers

Basic number formatting specifiers:

Specifier Type Format Output (Passed Double 1.42) Output (Passed Int -12400)
c Currency {0:c} $1.42 -$12,400
d Decimal (Whole number) {0:d} System.FormatException -12400
e Scientific {0:e} 1.420000e+000 -1.240000e+004
f Fixed point {0:f} 1.42 -12400.00
g General {0:g} 1.42 -12400
n Number with commas for thousands {0:n} 1.42 -12,400
r Round trippable {0:r} 1.42 System.FormatException
x Hexadecimal {0:x4} System.FormatException cf90

Custom number formatting:

Specifier Type Example Output (Passed Double 1500.42) Note
0 Zero placeholder {0:00.0000} 1500.4200 Pads with zeroes.
# Digit placeholder {0:(#).##} (1500).42
. Decimal point {0:0.0} 1500.4
, Thousand separator {0:0,0} 1,500 Must be between two zeroes.
,. Number scaling {0:0,.} 2 Comma adjacent to Period scales by 1000.
% Percent {0:0%} 150042% Multiplies by 100, adds % sign.
e Exponent placeholder {0:00e+0} 15e+2 Many exponent formats available.
; Group separator see below

The group separator is especially useful for formatting currency values which require that negative values be enclosed in parentheses. This currency formatting example makes it obvious:

     String.Format(”{0:$#,##0.00;($#,##0.00);Zero}”, value);

This will output “$1,240.00″ if passed 1243.50. It will output the same format but in parentheses if the number is negative, and will output the string “Zero” if the number is zero.

 

More details on the full range of format specifier strings here:
   http://eggins.com/files/folders/103/download.aspx


Posted May 17 2007, 01:04 AM by David

Comments

David wrote re: Powershell Number Formatting
on 10-13-2008 5:09 AM

I should have mentioned that you can also left or right align numbers to a given lenth. For example:

  1,22,33.3333,44444.4 | %{"Free: {0,9:#,#.00} GB" -f $_}

results in a the number being truncated to two fixed decimal places, comma separated for the thousands, right aligned and padded with spaces to 9 places.:

Free:      1.00 GB

Free:     22.00 GB

Free:     33.33 GB

Free: 44,444.40 GB

<Note that the padding shows better in a monospaced font>

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Copyright eggins.com, 1998 - 2008
Powered by Community Server (Non-Commercial Edition), by Telligent Systems