【Swift】少数点付きの四則演算

www.kuma-de.com

NSDecimalNumber を使うといいらしい。

        let str1 = "5.5"
        let double1 = 0.5

        let decimal1 = NSDecimalNumber(string: str1)
        let decimal2 = NSDecimalNumber(value: double1)

        let decimal3 = decimal1.adding(decimal2)

        or

        let decimal3 = decimal1.subtracting(decimal2)

        or

        let decimal3 = decimal1.multiplying(by: decimal2)

        or

        let decimal3 = decimal1.dividing(by: decimal2)

        let double2 = decimal3.doubleValue