Avatar ·

Rounding a Double to a specified number of decimal places in Swift

Hello everyone! I just started learning Swift and encountered a problem that seems simple, but I can't find an elegant solution.\n\nI have a variable of type `Double` that stores a time value in seconds, for example, `let elapsedTime: Double = 3.14159265`. I need to display this value to the user in a format with two decimal places, so it shows "3.14 seconds". But the number itself should remain a number, not a string, because I will use it in further calculations.\n\nI tried using `String(format:)` for display, but that gives a string, not a `Double`. For example:\n\n```swift\nlet value = 3.14159265\nlet formatted = String(format: "%.2f", value) // "3.14" — this is a String, not a Double\n```\n\nWhen I try to convert this string back to a `Double`,