How Can I Format a Series of Numbers as a Phone Number?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I format a series of numbers as a phone number in Windows PowerShell?

— HR

SpacerHey, Scripting Guy! AnswerScript Center

Hey, HR. You know, formatting is all about making things look a little nicer and a little more presentable. That’s why the Scripting Guy who writes this column always finds it strange when people ask him formatting questions. A little nicer and a little more presentable? Right now the Scripting Guy who writes this column is wearing a Homer Simpson T-shirt that says, “If something is hard it’s not worth doing.” He’s also wearing a ratty old long-sleeved shirt over the T-shirt; a pair of jeans; and a pair of three-year-old tennis shoes (although the shoelaces are brand-new). His entire ensemble is topped off by a half-smashed baseball hat that has Japan written across the front. And you want him telling you how to make things look a little nicer and a little more presentable?

Note. Here’s something really scary: if you think the Scripting Guy who writes this column looks bad, well, you should see the other Scripting Guys.

No, on second thought, maybe you shouldn’t.

But what the heck, HR; let’s see if we can help you with your formatting question anyway. Suppose you’ve type the following command into the Windows PowerShell console window:

$a = 5551234567

That’s nice, but what you’d really like to do is reformat $a so that it looks like this:

(555) 123-4567

So how can you do that? Why, by using this custom formatting string, of course:

$b = “{0:(###) ###-####}” -f $a

Let’s see if we can explain how this works. When it comes to formatting numbers and dates Windows PowerShell has no dedicated Cmdlets equivalent to the VBScript FormatDate or FormatNumber functions; instead, PowerShell taps into the .NET Framework when formatting values. The drawback to using .NET is that it requires the crazy-looking syntax shown above. The advantage to using .NET? You can create custom formats for displaying data in all sorts of different ways, including taking a series of numbers and displaying them as a phone number.

In order to understand this a little better let’s dissect our formatting command. What we’re doing with this command is applying a format to the variable $a; the gobbledygook between the double quote marks is the format being applied (details to follow), and the –f parameter is the signal we use to tell Windows PowerShell that we’re doing some formatting. The value to be formatted ($a) comes after the –f parameter, and the newly-formatted value is assigned to a variable named $b. And no, we don’t actually have to assign the newly-formatted value to a variable; if we wanted to we could use a command like this and simply display that value:

“{0:(###) ###-####}” -f $a

Now, what about the crazy-looking syntax? As you can see, the entire formatting string is nested first between a pair of double quote marks and then between a pair of curly braces: “{0:(###) ###-####}” We then have a 0 followed by a colon: 0:. That simply tells Windows PowerShell that we want to format the first item (item 0). For now consider that to be the default, and just use 0: without worrying too much about it.

The part that you do need to worry about (or at least the part you need to understand) is the actual formatting string itself: (###) ###-####. If you’re thinking, “You know, that kind of looks like a phone number,” well, that’s good; after all, it’s supposed to look like a phone number. In this formatting string the pound signs (#) each represent a digit; the parentheses and hyphens represent parentheses and hyphens. What we’re saying here is that we want to use the following format:

An open parentheses followed by three digits.

A closed parentheses and a space.

Three digits and a hyphen.

The final four digits.

That means we’re going to take a value like 5551234567 and display it like this:

(555) 123-4567

Lo and behold, there’s our phone number.

As long as we’re on the subject here are a couple of other custom formatting commands you might find useful. For example, suppose you want to format a value using leading zeroes. Well, here’s an example for you:

$a = 123
“{0:000000}” -f $a

What are you going to get when you run these two commands? This:

000123

How about a fixed number of decimal places? Are three decimal places enough? Okey-doke:

$a = 123.456789
“{0:#.000}” -f $a

That will result in output that looks like this (note that the value has been rounded up to the nearest decimal place):

123.457

Here’s one that uses U.S. currency as the format; note the $ symbol included at the beginning of the formatting string:

$a = 123.45
“{0:$#.00}” -f $a

That yields a value the looks like this:

$123.45

You can do similar things for dates. But that’s another story for another day.

We hope that helps, HR. And remember, if you ever need any other advice on making things look nicer and a little more presentable the Scripting Guys will be here. Take interior decorating, for example. The Scripting Guy who writes this column has an office that has nothing hanging on the walls other than a map of the world, a map which is hidden behind his door and is hanging there simply because the previous tenant left it hanging there. He has no plants, no photographs, no personal mementoes, nothing.

Note. Well, OK, with one exception: taped to his door is a fortune from a Chinese fortune cookie that says, “Others take notice of your radiance. Share your happiness.” The other Scripting Guys agree that they’ve never heard a more apt description of the Scripting Guy who writes this column.

Best of all, following the last office move he didn’t even bother to unpack his things; instead, two months after the move all his unopened boxes remain piled up in one corner of the office. If you’d like to have an office like that, well, just let him know; we’re sure he’d be happy to help. After all, sharing his happiness is what he does best.

0 comments

Discussion is closed.

Feedback usabilla icon