swiftmultiplicationnsdecimalnumber

Swift - Multiply two NSDecimal Numbers


I am currently attempting to multiply two NSDecimal Numbers, is this possible in Swift?

I found the method 'decimalNumberByMultiplyingBy' on the Apple Dev pages but I have used it as below and I am receiving a 'Bad access' error.

Purchase Price and Quantity are both NSDecimalNumbers in the Item class and are definitely populated (with purchase price of 35.99 and quantity of 30)

var one = item.purchasePrice
var two = item.quantity

let three: NSDecimalNumber = one.decimalNumberByMultiplyingBy(two)

I have tried it multiple ways but all the tutorials seem to be for Objective C.

Thanks.


Solution

  • that works perfectly.

    var numberOne: NSDecimalNumber = NSDecimalNumber(float: 35.99)
    var numberTwo: NSDecimalNumber = NSDecimalNumber(int: 30)
    let numberThree: NSDecimalNumber = numberOne.decimalNumberByMultiplyingBy(numberTwo)
    
    print(numberThree)
    

    the output on the console is:

    1079.7000503540041728
    

    NOTE: your var's type (three) may not be NSDecimalNumber instances, that is why you got the exception.