Just trying my first simple attempt on a calculation in swift2, but I can't seem to get it right.
I have a UITextField input called ValueA
I want to du the following calculation on that input (i can have decimals)
(((ValueA * ValueA)+3)*1.36)
The result must return a number with up to two decimals.
I have tried the following:
let kw = 1.36
let three: Double = 3
var a: Double = NSString(string: ValueA.text!).doubleValue
var b: Double = NSString(string: ValueB.text!).doubleValue
var answer:Double = (((a * a) + three) * kw)
let answerFormatter = NSNumberFormatter()
answerFormatter.minimumFractionDigits = 2
answerFormatter.maximumFractionDigits = 2
let Ranswer = answerFormatter.stringFromNumber(answer)!
Result.text = Ranswer`
It kindda works, but sometimes my simulator crashes and sometimes it gets me the right answer but with more decimals as zeroes eg: 45.23000000 (instead of 45.23)
Can someone clean up my code? The answer need to go back into a textfield. Remember I am a total newbee in swift :)