Avatar ·

Rounding Double to a specified number of decimal places in Swift

Hi! I'm new to Swift and encountered a problem when working with Double numbers. I need to output time values (NSTimeInterval) with a certain precision, for example, to two decimal places. But Swift doesn't seem to provide a simple way to do this. Here's an example of my problem: ```swift let duration: TimeInterval = 3.14159265 print(duration) // Outputs 3.14159265, but I need 3.14 let price: Double = 19.995 // Need to round to 20.00 for UI display ``` I tried using `String(format:)`: ```swift let formatted = String(format: "%.2f", duration) print(formatted) // Outputs "3.14", but it's a string, I need a Double! ``` The problem is that I need to get exactly a Double number with rounding, not a string. For example, for further mathematical o