• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Tachytelic.net

  • Get in Touch
  • About Me

How to add Thousands separators to a number with Powershell

October 8, 2019 by Paulie Leave a Comment

When working with long numbers, they are much easier to read with the thousand separators included. The Powershell format operator makes it simple.

$Number = 2109999.86
'{0:N0}' -f $Number
'{0:N2}' -f $Number

Produces the following output:

As you can see from the above, the first example produces a whole number with no decimal places, and the second one includes two decimal places.

In practice, when I needed to do this, the number provided to me was actually a string read from a CSV File, so although you can use the format operator, it won’t do anything:

Image showing Powershell trying to add thousand separators to a number stored as a string.

The string needs to be cast as a number before you can use the format operator in this way:

$StringNumber = "10456.21" 
$($StringNumber -as [decimal]).ToString('N2')

Produces a number with separators:

You can use the format operator for all sorts of things. You might like this post on how to format leading zeros with the format operator.

Filed Under: Scripts & Utilities Tagged With: Powershell

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Link to my LinkedIn Profile
Buy me a coffee

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 254 other subscribers.

Go to mobile version