From the code below I want convertedN
to be to be 99999999
but instead I get 99999998
. The problem is that there is a rounding error before I set n
. What can I do to get the result I want?
let amount = ".99999999"
let tmpFormatter = NSNumberFormatter()
tmpFormatter.maximumFractionDigits = 8
let n = tmpFormatter.numberFromString(amount)
let decimalAmount = NSDecimalNumber(decimal: n!.decimalValue)
let convertedN = (decimalAmount.decimalNumberByMultiplyingBy(NSDecimalNumber(unsignedLongLong: 100000000))).unsignedLongLongValue
Try like this:
let amount = ".99999999"
let decimalAmount = NSDecimalNumber(string: amount)
let convertedN = decimalAmount.decimalNumberByMultiplyingBy(NSDecimalNumber(string: "100000000")).unsignedLongLongValue // 99,999,999