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

1
  • What is the stacktrace you get from the simulator crash? Could you post it here? Commented Jun 23, 2016 at 6:57

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Multiplying and dividing by 100 is no appropriate method to format a floating point number. Formatting with some format specifier is. :)
@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!
Can't make it work. Build fails. It should be a*a. I have not made the code where b is used, yet :)
Why would the build fail? share the error? and also the code that you tried
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")
}

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.