Both ChatGPT & Copilot have failed me. I am seeking to add padding to a time that gives me two digits for whole seconds and three digits for fractional seconds.
So, given $time which is produced by
$timer = [Stopwatch]::StartNew()
$timer.Stop()
$time = $timer.Elapsed.TotalSeconds
AI suggests things like
$time = [Math]::Round($time, 3)
$time = "{0:000.000}" -f $time
When I try to use the resulting time like this
"$($time): $function"
I get a single 0 to the left of the decimal, and no trailing zeros added.
Where am I (and AI) going wrong?
TotalSecondsvalue exceeds 100.0?"{0:00.000}" -f $timeis the correct solution (you can skipMath.Round, formatter will take care of it for you) - perhaps you've typed$timeas a[decimal]or[double]somewhere earlier in the script? Another explanation could be that because you stop the timer almost immediately, the elapsed time is <5ms, which would indeed result in0.000. Does it work as expected if you sleep + use a new variable?[Double], because($timer.Elapsed.TotalSeconds).GetType().Fullname)said that's what the timer was providing. Do I need to work from a[DateTime]instead? Or is there another approach I need when formatting a double?'{0:00.000}' -f $timeto some variable that isn't typed[double](appending to a[List[string]]or[List[psobject]]would work)