How to get the integer part and fractional part of a number in Swift

Viewed 16871

This is a super basic question, but, I can't seem to find an answer in Swift.

Question:

How do I get the whole integer part and fractional part (to the left and right of the decimal point respectively) of a number in Swift 2 and Swift 3? For example, for the number 1234.56789 —

How do I get the integer part 1234.56789 ?

How do I get the fractional part 1234.56789 ?

8 Answers

Swift 4.xm, 5.x complete solution: I credit @Thomas solution also I'd like to add few things which allow us to use separated parts in 2 string part. Especially when we want to use 2 different UILabel for main and decimal part of the number.

Separated integer and decimal label

Extension below is also managing number quantity of decimal part. I thought it might be useful.

UPDATE: Now, it is working perfectly with negative numbers as well!

public extension Double{
func integerPart()->String{
    let result = floor(abs(self)).description.dropLast(2).description
    let plusMinus = self < 0 ? "-" : ""
    return  plusMinus + result
}
func fractionalPart(_ withDecimalQty:Int = 2)->String{
    let valDecimal = self.truncatingRemainder(dividingBy: 1)
    let formatted = String(format: "%.\(withDecimalQty)f", valDecimal)
    let dropQuantity = self < 0 ? 3:2
    return formatted.dropFirst(dropQuantity).description
}

No need for extensions. Swift already has built in function for this.

let aNumber = modf(3.12345)
aNumber.0 // 3.0
aNumber.1 // 0.12345

This will definitely work for you in swift 5 and 4

let number = 3.145443
let integerValue = String(format: "%.0f", number)
let integerValue1 = String(format: "%.1f", number)
let integerValue2 = String(format: "%.2f", number)
print(integerValue)
print(integerValue1)
print(integerValue2)

//Output
3
3.1
3.14

modf() works bad with numbers > 1 billion

@Trevor behaves like modf()

@Andres Cedronius example behaves like modf() too (i tried modify it)

@Irshad Ahmed solution is nice

there is some convenience convertions

For some reason you need more control, check out https://developer.apple.com/documentation/foundation/numberformatter

extension Double {

    /// 1.00234 -> 1.0
    var integerPart: Double {
        return Double(Int(self))
    }

    /// 1.0012 --> 0.0012
    var fractionPart: Double {
        let fractionStr = "0.\(String(self).split(separator: ".")[1])"
        return Double(fractionStr)!
    }

    /// 1.0012 --> "0.0012"
    var fractionPartString: String {
        return "0.\(String(self).split(separator: ".")[1])"
    }

    /// 1.0012 --> 12
    var fractionPartInteger: Int {
        let fractionStr = "\(String(self).split(separator: ".")[1])"
        return Int(fractionStr)!
    }

}
print(1000.0.integerPart)  // 1000.0
print(1000.0.fractionPart) // 0.0
print(1000.1.integerPart)  // 1000.0
print(1000.2.fractionPart) // 0.2
print(1_000_000_000.1.integerPart) // 1000000000.0
print(100_000_000.13233.fractionPart) // 0.13233

var specimen0:Double = 1234.56789

var significant0 = Double.IntegerLiteralType(specimen0) // result is an integer
var fractionals0 = specimen0 - Double(significant0)


var specimen1:Float = -1234.56789

var significant1 = Float.IntegerLiteralType(specimen1) // result is an integer
var fractionals1 = specimen1 - Float(significant1)


var specimen2:CGFloat = -1234.56789

var significant2 = CGFloat.IntegerLiteralType(specimen2) // result is an integer
var fractionals2 = specimen2 - CGFloat(significant2)

These are all built in as of Swift 5.3, I am sure even earlier...

Related