#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
133,903,155.00

Print the number without precision digits.

PS> '{0:N0}' -f $number
133,903,155

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

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

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

PS> $number.ToString('N0')
133,903,155

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
,

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: