-1

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?

8
  • 3
    "a time that gives me two digits for whole seconds" - what behavior are you hoping for if the TotalSeconds value exceeds 100.0? Commented Apr 2 at 13:20
  • I don't expect it to exceed 99 seconds, and if it does once in a while I'll just live with it, or push to three digits. But I am just using it to profile performance of some functions as I refactor some things, so it's not super important. But constantly getting one or two times with only two digits when the vast majority have had three, is making a quick understanding of the numbers problematic. Commented Apr 2 at 13:25
  • FWIW "{0:00.000}" -f $time is the correct solution (you can skip Math.Round, formatter will take care of it for you) - perhaps you've typed $time as 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 in 0.000. Does it work as expected if you sleep + use a new variable? Commented Apr 2 at 13:28
  • $time is being passed as a [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? Commented Apr 2 at 13:34
  • 1
    Okay there's your explanation - assign the output of '{0:00.000}' -f $time to some variable that isn't typed [double] (appending to a [List[string]] or [List[psobject]] would work) Commented Apr 2 at 13:50

1 Answer 1

3

Using the format string 00.000 is indeed correct, but since you're assigning the resulting string back to $time (which appears to have a [double] type attribute attached) it gets parsed and converted back to a [double] immediately, which is why it suddenly renders as just 0 when you later use it in an expandable string.

Assign the output from the -f operation to a variable that isn't [double]-typed and it'll work just fine:

$timer = [Stopwatch]::StartNew()
$timer.Stop()
[double]$time = $timer.Elapsed.TotalSeconds

$timestamp = '{0:00.000}' -f $time

"${timestamp}: ... is how long it took"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.