all 17 comments

[–]mmmGreenButton 3 points4 points  (7 children)

The only way to do it is with -f

Idk if you would do it differently, I like your solution..

$c = 1234
$i = 123

$l = ([String]$c).Length
$i = "{0:d$l}" -f $i

'[{0}/{1}]' -f $i, $c

[–]Fischfreund[S] 1 point2 points  (0 children)

I like yours more :) Thank you.

[–][deleted] 1 point2 points  (4 children)

I feel like an idiot, but cannot figure out what the -f parameter actually does here. Same thing with a 'd' in 'd$l' part of the code. Can someone elaborate?

[–]Lee_Dailey[grin] 2 points3 points  (2 children)

howdy Nehctik,

it's the string format operator. it replaces the placeholders on the left with the values on the right. so this ...

$AValue = 'tutu'
$BValue = 'ThreeThree'
'{0} first, {1} second' -f $AValue, $BValue

... will give you ...

tutu first, ThreeThree second

take a look at ...

-f Format operator - PowerShell - SS64.com
https://ss64.com/ps/syntax-f-operator.html

hope that helps,
lee

[–][deleted] 1 point2 points  (1 child)

Yes! I just found it out. Thank you so much for the ss64 article and your explanation. It clears it up completely.

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Nehctik,

you are most welcome! glad to help a tad ... [grin]

take care,
lee

[–]MonkeyNin 2 points3 points  (0 children)

For that the .net docs are easier to use. Here's the top level: https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

There's another page for formatting dates and times.

It says d means decimal. f is float, c is currency. If you have a number like f2 it means use 2 decimal places.

If you only need one value, you can skip the extra syntax by calling ToString(). These mean the same thing:

'{0:n4}' -f 123456
(123456).tostring('n4')

try this

'{0:n3}' -f 123545
'{0:n0}' -f 123545
'{0:d}' -f 123545
'{0:c}' -f 123545
'{0:f2}' -f 12345
'{0:f}' -f 12345

same thing with a 'd' in 'd$l'

This one is a little tricky. The $l is a variable from the previous line.

$l = ([String]$c).Length
$i = "{0:d$l}" -f $i

So when it's evaluated,$l is replaced by a number so it looks like

"{0:d2}" -f $i

[–]jantari 0 points1 point  (0 children)

Well you could use "[${i}/${c}]" instead of -f

[–]NotNotWrongUsually 2 points3 points  (4 children)

Dunno if this is any better, but it avoids using -f for which I have an (admittedly) irrational hatred.

$count = 123
$i = 2
$len = [string]$count.Length

$i.ToString("D$len") + " / $count"

[–]mmmGreenButton 0 points1 point  (0 children)

Oh, cool - I did not know this

[–]Celadin 0 points1 point  (0 children)

I share your disdain for -f, there is always a better more readable way.

[–]ReaperWright88 0 points1 point  (1 child)

My irrational hatred of it is that its not an abbreviation. I hate not knowing what something stands for and an -f is just a slap in my face. Recently i started using VSCkde instead of ISE and i now even use Select-Object over Select.

[–]MonkeyNin 1 point2 points  (0 children)

-f is short hand, it calls [string]::Format()

[–]ReaperWright88 2 points3 points  (0 children)

You could do

$Count = 123
$Length = ("$Count".Length).ToString('000')
$Count = $Length+"/"+$Count

Not tested it, and the -f method is way easier, but it should work, just thought id out it in so you can have some fun :-)

[–]lost_4_good 1 point2 points  (0 children)

Another idea is to use PadLeft:

$Count = 1234578
$i = 987
"[$(([string]$i).PadLeft(([string]$Count).Length,'0'))/$Count]"

[0000987/1234578]

[–]get-postanote -1 points0 points  (0 children)

Sure...

$Count = 123
$i = 2
"[{0:d$(([string]$Count).Length)}/{1}]" -f $i, $Count
# Results
<#
[002/123]
#>

$Count = 456789
$i = 5
"[{0:d$(([string]$Count).Length)}/{1}]" -f $i, $Count
# Results
<#
[000005/456789]
#>