I'm trying to separate the decimal and integer parts of a double in swift. I've tried a number of approaches but they all run into the same issue...
let x:Double = 1234.5678 let n1:Double = x % 1.0 // n1 = 0.567800000000034 let n2:Double = x - 1234.0 // same result let n3:Double = modf(x, &integer) // same result
Is there a way to get 0.5678 instead of 0.567800000000034 without converting to the number to a string?
By using round(_:) , ceil(_:) , and floor(_:) you can round Double and Float values to any number of decimal places in Swift.
double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision.
Shift the decimal of the given value to the given decimal point by multiplying 10^n. Take the floor of the number and divide the number by 10^n. The final value is the truncated value.
You can use truncatingRemainder
and 1
as the divider.
Returns the remainder of this value divided by the given value using truncating division.
Apple doc
Example:
let myDouble1: Double = 12.25 let myDouble2: Double = 12.5 let myDouble3: Double = 12.75 let remainder1 = myDouble1.truncatingRemainder(dividingBy: 1) let remainder2 = myDouble2.truncatingRemainder(dividingBy: 1) let remainder3 = myDouble3.truncatingRemainder(dividingBy: 1) remainder1 -> 0.25 remainder2 -> 0.5 remainder3 -> 0.75
Same approach as Alessandro Ornano implemented as an instance property of FloatingPoint protocol:
Xcode 11 • Swift 5.1
import Foundation extension FloatingPoint { var whole: Self { modf(self).0 } var fraction: Self { modf(self).1 } }
1.2.whole // 1 1.2.fraction // 0.2
If you need the fraction digits and preserve its precision digits you would need to use Swift Decimal
type and initialize it with a String
:
extension Decimal { func rounded(_ roundingMode: NSDecimalNumber.RoundingMode = .plain) -> Decimal { var result = Decimal() var number = self NSDecimalRound(&result, &number, 0, roundingMode) return result } var whole: Decimal { rounded(sign == .minus ? .up : .down) } var fraction: Decimal { self - whole } }
let decimal = Decimal(string: "1234.99999999")! // 1234.99999999 let fractional = decimal.fraction // 0.99999999 let whole = decimal.whole // 1234 let sum = whole + fractional // 1234.99999999 let negativeDecimal = Decimal(string: "-1234.99999999")! // -1234.99999999 let negativefractional = negativeDecimal.fraction // -0.99999999 let negativeWhole = negativeDecimal.whole // -1234 let negativeSum = negativeWhole + negativefractional // -1234.99999999
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With