In my swift program, I have a really long decimal number (say 17.9384693864596069567) and I want to truncate the decimal to a few decimal places (so I want the
Copy this code into your application...
import Foundation
func truncateDigitsAfterDecimal(number: Double, afterDecimalDigits: Int) -> Double {
if afterDecimalDigits < 1 || afterDecimalDigits > 512 {return 0.0}
return Double(String(format: "%.\(afterDecimalDigits)f", number))!
}
Then you can call this function like:
truncateDigitsAfterDecimal(number: 45.123456789, afterDecimalDigits: 3)
Will produce the following:
45.123