The Crystal Programming Language Forum

Time display in seconds: xxxx.yyy

I wasn’t sure how to format this from the docs.

I’m doing timing differences, and it displays like this.

t1 = Time.monotonic
---
---
puts Time.monotonic - t1 =>  00:00:00.382677856

but I want to display just seconds (no matter how large), with between 3-6 decimal places

puts Time.monotonic - t1 =>  wxyz.abcdef

Try (Time.monotonic - t1).total_seconds.

See https://crystal-lang.org/api/Time/Span.html#total_seconds:Float64-instance-method.

1 Like

Thanks, that’s so nice and clear!
I thought I had to mess around with C style print format codes.

How do I limit decimal display to 6 (etc) places?

Call value.round(6)

1 Like

Perfect!

te = (Time.monotonic - ts).total_seconds.round(6)

No, this is perfect :rofl:

Time.monotonic
  .-(ts)
  .total_seconds
  .round(6)
4 Likes