#PSTip How to add thousands separators to a number

Say you have a number like: 133903155, how quickly can you digest it? It’s much easier to read when it’s formatted as 133,903,155. One way to convert the number to a readable string is using the -f Format operator:

PS> $number = 133903155
PS> '{0:N}' -f $number

Print the number without precision digits.

PS> '{0:N0}' -f $number

Under the hood PowerShell is utilizing the String.Format method to format the number (see also Composite Formatting).

PS> [string]::Format('{0:N0}',$number)

Another way to format numbers would be by using an object’s ToString() method.

PS> $number.ToString('N0')

Note that the symbol that separates groups of integers is determined by the current culture used on your system (session):

PS> (Get-Culture).NumberFormat.NumberGroupSeparator
Filed in: Columns, Tips and Tricks Tags: , ,

3 Responses to "#PSTip How to add thousands separators to a number"

  1. adotms says:

    BR BR BR >_<

  2. js2010 says:

    function com { param([Parameter(ValueFromPipeline=$True)]
    process { ‘{0:n0}’ -f $number } }

    PS /Users/me> 12345 | com

    PS /Users/me> com 12345

Leave a Reply

Submit Comment

© 9316 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: