Soldato
- Joined
- 25 Mar 2004
- Posts
- 15,991
- Location
- Fareham
Hi all,
Is there a way in PowerShell to convert a one digit number to a two digit number?
For example:
Loops through numbers 1 to 10 and returns them as values $number.
when run you get this:
1
2
3
4
5
6
7
8
9
10
I want this returned instead:
01
02
03
04
05
06
07
08
09
10
So if number is less than 9 it adds a 0 on the front.
I was thinking something like:
If you run this you do get:
01
02
03
04
05
06
07
08
09
10
But it doesn't seem very elegant, is there a better way?
Thanks!
Is there a way in PowerShell to convert a one digit number to a two digit number?
For example:
Code:
$count = 1..10 ; foreach ($number in $count) {$number}
Loops through numbers 1 to 10 and returns them as values $number.
when run you get this:
1
2
3
4
5
6
7
8
9
10
I want this returned instead:
01
02
03
04
05
06
07
08
09
10
So if number is less than 9 it adds a 0 on the front.
I was thinking something like:
Code:
$count = 1..10 ; foreach ($number in $count)
{
if ($number -le 9)
{
$number = "0" + $number
}
$number
}
If you run this you do get:
01
02
03
04
05
06
07
08
09
10
But it doesn't seem very elegant, is there a better way?
Thanks!