This article will show how you can round a double value to X number of decimal places in Swift.
Rounding a Double value to a specific number of decimal places is a common task in Swift. There are various ways to achieve this, and the community has provided different solutions.
Using Swift’s round Function
The most straightforward method involves using Swift’s round function. Here’s an example to round a Double to three decimal places:
let x = 1.23556789 let roundedValue = Double(round(1000 * x) / 1000) print(roundedValue) // Output: 1.236
This approach involves multiplying the original value by 1000, rounding it, and then dividing the result by 1000.
Extension for Swift 2, 3
For a more general solution, you can use an extension:
extension Double {
func roundToPlaces(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
// Example usage for Swift 3
let roundedValue = Double(0.123456789).roundToPlaces(4)
print(roundedValue) // Output: 0.1235
Swift 5: Different Styles for Rounding
Swift 5 provides various styles for rounding a Double. Here are nine different approaches:
// 1. Using FloatingPoint rounded() method
let roundedValue1 = (0.6844 * 1000).rounded() / 1000
let roundedValue2 = (0.6849 * 1000).rounded() / 1000
// 2. Using FloatingPoint rounded(_:) method
let roundedValue3 = (0.6844 * 1000).rounded(.toNearestOrEven) / 1000
let roundedValue4 = (0.6849 * 1000).rounded(.toNearestOrEven) / 1000
// 3. Using Darwin round function
import Foundation
let roundedValue5 = round(0.6844 * 1000) / 1000
let roundedValue6 = round(0.6849 * 1000) / 1000
// 4. Using a Double extension custom method built with Darwin round and pow functions
extension Double {
func roundToDecimal(_ fractionDigits: Int) -> Double {
let multiplier = pow(10, Double(fractionDigits))
return Darwin.round(self * multiplier) / multiplier
}
}
let roundedValue7 = 0.6844.roundToDecimal(3)
let roundedValue8 = 0.6849.roundToDecimal(3)
// 5. Using NSDecimalNumber rounding(accordingToBehavior:) method
import Foundation
let roundedValue9 = NSDecimalNumber(value: 0.6844).rounding(accordingToBehavior: behavior)
let roundedValue10 = NSDecimalNumber(value: 0.6849).rounding(accordingToBehavior: behavior)
// 6-9. Using various String and NumberFormatter approaches
// (Details provided in the original community answers)
print(roundedValue1, roundedValue2, roundedValue3, roundedValue4, roundedValue5, roundedValue6, roundedValue7, roundedValue8, roundedValue9, roundedValue10)
Choose the method that best fits your needs and preferences.
Swift 5.7 and Xcode 14
For Swift 5.7 and Xcode 14, you can use the String(format:) method for rounding:
let pi: Double = 3.14159265358979 let roundedPi = String(format: "%.2f", pi) print(roundedPi) // Output: 3.14
Swift 3.0/4.0/5.0 with Specific Decimal Places
To round a Double to a specific number of decimal places in Swift 3.0/4.0/5.0, you can use the following code:
let doubleValue: Double = 123.32565254455 let roundedValue1 = String(format: "%.f", doubleValue) // Output: 123 let roundedValue2 = String(format: "%.1f", doubleValue) // Output: 123.3 let roundedValue3 = String(format: "%.2f", doubleValue) // Output: 123.33 let roundedValue4 = String(format: "%.3f", doubleValue) // Output: 123.326
These examples demonstrate rounding a Double to integers or a specific number of decimal places.
Swift 3.0 and Xcode 8.0
In Swift 3.0 and Xcode 8.0, you can use an extension to round a Double to a specified number of decimal places:
extension Double {
func roundTo(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}
// Example usage
let doubleValue = 3.567
let roundedValue = doubleValue.roundTo(places: 2)
print(roundedValue) // Output: 3.57
Swift 4, Xcode 10
In Swift 4 with Xcode 10, you can use the String(format:) method to round a Double:
yourLabel.text = String(format: "%.2f", yourDecimalValue)
This example sets the text of a label with a Double value rounded to two decimal places.
Swift 4, Xcode 10 (Alternative)
Another Swift 4 example using a string conversion:
let roundedString = String(format: "%.3f", 1.543240952039) print(roundedString) // Output: 1.543
This code converts a Double to a string with three decimal places.
Conclusion
Choose the method that suits your requirements and coding style. Rounding a Double in Swift can be done in various ways, and the provided examples cover a range of approaches.