Skip to content

add leading zeros to a number

2011/06/13

To add leading spaces zeros to a string representation of a number:

17 | %{"0000$_".substring("0000$_".length -4)}

So sorting is possible.

After looking at the format strings of dot.net I found a more elegant solution

PS C:\> "{0:D4}" -f 22
 0022
Advertisements

From → powershell, strings

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: