2

#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
,
Filed in: Columns, Tips and Tricks Tags: , ,

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

  1. adotms says:

    BR BR BR >_<

Leave a Reply

Submit Comment

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