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)