0

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 :)

vrwim
  • 13,020
  • 13
  • 63
  • 118
KristianC
  • 35
  • 8

2 Answers2

1

You can use something like this

let kw = 1.36
let three: Double = 3
var a = Double(ValueA.text!)!
var b = Double(ValueB.text!)!
var answer = (((a * a) + three) * kw)


 let Ranswer = String(format:"%.2f", answer)
Result.text = Ranswer

And are you sure you wanna do a * a? and not a * b. Just making sure, because you arent using b anywhere

you can also use

let Ranswer = Double(round(100 * answer)/100)
Result.text = String(Ranswer)

Source of information : link

Community
  • 1
  • 1
Akshansh Thakur
  • 5,163
  • 2
  • 21
  • 38
  • Multiplying and dividing by 100 is no appropriate method to format a floating point number. Formatting with some format specifier is. :) – Eiko Jun 23 '16 at 09:44
  • @Eiko I'm with you on this! I'm not the original one to answer this. But I included this because I absolutely love the creativity of the guy who came up with this method (credits to him through the link) I have changed the answer though, thanks for pointing out! – Akshansh Thakur Jun 23 '16 at 09:47
  • Can't make it work. Build fails. It should be a*a. I have not made the code where b is used, yet :) – KristianC Jun 24 '16 at 07:58
  • Why would the build fail? share the error? and also the code that you tried – Akshansh Thakur Jun 24 '16 at 08:01
0

You are assuming that the always enter a valid number into ValueA and ValueB. That's one possible source for the crash. Better check that the text can be converted to numbers.

Try this (not tested):

let kw = 1.36
let three = 3.0

if let a = Double(ValueA.text!), b = Double(ValueB.text!) {
    let answer = (((a * a) + three) * kw)

    let answerFormatter = NSNumberFormatter()
    answerFormatter.minimumFractionDigits = 2
    answerFormatter.maximumFractionDigits = 2

    let Ranswer = answerFormatter.stringFromNumber(answer)!
    Result.text = Ranswer
} else {
    print("You must enter a number into ValueA and ValueB")
}
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • That worked perfect. I had made the UITextfields as show numeric keyboard, så that is why I didn't check for numerics values. Where does the Print go? I can't see it anywhere in the simulator? Is there anyway to use comma in this calculation? eg 5,5 instead of 5.5? When I use comma nothing happens – KristianC Jun 24 '16 at 07:53
  • I think `Double` uses the default separator for your locale when converting string to number. Use `NSNumberFormatter` if you want to customize that. `print` will go to the Debug Console. Press `Cmd + Shift + Y` to open it – Code Different Jun 24 '16 at 11:41