79329455

Date: 2025-01-04 19:21:53
Score: 0.5
Natty:
Report link

Based on comments from @sweeper, I turned the duration into a TimeInterval and then simply formatted the TimeInterval as a number:

extension Duration {
    func timeInterval() -> TimeInterval{
        return TimeInterval(components.seconds) + Double(components.attoseconds)/1e18
    }
}

extension Duration {
    func formatAsSeconds(precision: Int = 0) -> String{
        return timeInterval().formatted(.number.precision(.fractionLength(precision)))
    }
}

And with those extensions, I can format the Duration like so:

myDuration.formatAsSeconds(precision: 2)
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @sweeper
  • Self-answer (0.5):
Posted by: Brian