Learn Courses My Dashboard

Issues with the mathematic operations

Hey there, I’m trying to do a food app in which I try to multiply three input user data among them and with other double numbers. Any clue?

Welcome to the community!!

Do you have code that you’ve already written but isn’t working correctly?

If you can paste that in the forum that can give us a better idea of what part of that you’re struggling with.

Paste your code in as text, rather than providing a screenshot.

To format the code nicely, place 3 back-ticks ``` on the line above your code and 3 back-ticks ``` on the line below your code. The 3 back-ticks must be the ONLY characters on the line. The back-tick character is located on the same keyboard key as the tilde character ~ (which is located below the Esc key). You can also highlight an entire code block and click the </> button on the toolbar to wrap the block for you.

This also makes it easier for anyone assisting as they can copy the code and carry out some testing.

@markk

If you are using a combination of Integer inputs and Floating point inputs where you are multiplying them together, then you need to convert them to the same type, preferably Double if one of them is a decimal quantity.

So let’s say you have a recipe which lists the quantities for a single serve and you want to get the quantities of each for a number of people.

For example:

let cupsOfMilk = 1.5  //  Xcode will infer as a Double

let people = 5  // Xcode will infer as an Integer

let totalCupsOfMilk = cupsOfMilk * Double(people)

print(totalCupsOfMilk) 

Thanks, but the thing is I need to multiply the user input and not an specific number. Sorry maybe it’s to obvious but I’m quite new in programming.

This is:

You need to convert the user’s input, which is a String into an Int or Double or some other type

And then do the math

What are Pes, Altura, and Edat?? Based on the error they look like the name of UITextFields

And how can I do that?

Yes exactly are three different UITextFields which I need to multiply and with other numbers such as 9.99 and 5.75.

First you need the value from the text field, I’ll use Pes for this example, but they will all be the same, also btw I would rename this to pesTexField, variables shouldn’t start with uppercase letters, and naming it text field helps you know that it’s a UITextField

let stringAmount = pesTextField.text // this is a string, because the .text property, returns the string of what’s typed in the text field

let doubleAmount = Double(stringAmount) // this is now a double, as long as the user typed something valid, ie doesn’t type “five” but a real number

let multiplier = Double(5) // just a random number I thought of

let value = doubleAmount * multiplier

You could put this code in a function after a button is clicked