I am trying to make a calculator app in swift as a practise for my IOS development course. for that app I am trying to append a number to an existing double or integer after the user pressed on a specific button, but I don't know how.
if the user pressed on lets say a 5, then I want that the code should append that 5 to the numbers he pressed before
for instance:
the user has typed the following number:
6797.890
and now he wants to add the 5 to the existing number so that the number would be:
6797.8905
I really don't know how to do this in the code, and I really appreciate it if someone could help me by showing how or giving some resource website's for this problem
thanks a lot!
Benji
I know you mentioned that you are supposed to use an Int or Double, but I would try and use NSDecimalNumber and String if you can. It will give you the precision you want and has a convenient way to turn the string into an NSDecimalNumber, NSDecimalNumber(string: "6797.8905"), as well as back to a string, number.stringValue. You can enteredAmount != NSDecimalNumber.notANumber just in case your user input is incompatible.
You must process the input as a string. If you process it as a double, the trailing zero will be lost and your code will not be able to distinguish between adding "5" to 6797.890 and adding "5" to 6797.89
Only convert from String to Double in one direction (from input to data) after displaying the initial value.
Related
I have a list of Text views that include a year saved as an int. I'm displaying it in an interpolated string:
Text("\($0.property) \($0.year) \($0.etc)")
The problem, it adds a comma to the int, for example it displays 1,944 instead of the year 1944. I'm sure this is a simple fix but i've been unable to figure out how to remove the comma. Thanks!
There is explicit Text(verbatim:) constructor to render string without localization formatting, so a solution for your case is
Text(verbatim: "\($0.property) \($0.year) \($0.etc)")
Use Text(String(yourIntValue)) if you use interpolation you need to cast it as a string directly. If you allow the int to handle it, it shows with a ,.
So to recap.
let yourIntValue = 1234
Text(String(yourIntValue)) // will return `1234`.
Text("\(yourIntValue)") // will return `1,234`.
I use the built-in format parameter. It's useful for formatting well beyond just this one specific usage (no commas).
Text("Disk Cache \(URLCache.shared.currentDiskUsage,
format: .number.grouping(.never))"))
This is going to sound very weird but it will make the code for this app very compact.
Is there a way to call a .operator conditionally. Here is an example
say I have a class with three values that I can do a . on.
class A {
int intOne;
int intTwo;
int intThree;
}
to get the intOne in class A you can do A.intOne right....
but what if you you wanted the string at the right of the . to conditionally be there.
A."conditionalvariable"
so if a user clicks a button say button A then the conditional variable will be A.intOne.
and if the user clicks button b then the value of "conditionalvariable" will be intTwo and therefore you will be getting the data.
please assume that I am not stupid, and I am needing this exact thing for a specific use case. If you post an answer that is not this exact solution it will not be accepted.
example:
var a;
switch (name) {
case "intOne":
a = A.intOne;
break;
...
}
//this is not an acceptable answer
I realize that this would be an "answer" to this question, but it actually isn't I need the exact thing stated because I am using streams.
here is that use case I was talking about. I could explain away all day as to why I need it this way, but either way this either does exist or doesn't so a simple no this doesn't exist is an acceptable answer.
return StreamProvider.value(
value: classDataNotif.homework,
)
based on what is clicked before this widget the "homework" will need to change to "notes", or "tests".
You cannot do a switch above this because it will be calling the stream to early and cause the widget to crash. doing a switch inside the widget and copying code over would defeat the purpose. in order to make the code as compact and as easy to write as possible i need the string at the right of the . operator to change.
thanks in advance :)
I work with an array of string, each string var is a coded object.
I want to decode the object, when I print a string var I get something structured like that :
"firstName=\"Elliot\" lastName=\"Alderson\" gender=\"male\" age=\"33\",some description I also need to get"
Is that a standard format to store key value properties ? I can't find anything on internet. The keys are always the same so that's not a big deal to get theses values as a dictionary but I would like to know if there is like a best practice method to get theses data instead of just searching for each key and then reach value from the first quote to the second one (for each value)
Because my file is 30000 lines so I better choose the more optimized way.
Thanks !
If I have a number that is over 1,000, so that it has a comma in it, how can I either strip out the commas, or convert this number into a Double, so that I can run mathematical operations on it?
Right now, I have the following code:
let oldBFTally: Double = Double(plBFTally.text!)!
let newBFTally: Double = round(1000 * (rawValue + oldBFTally) / 1000)
This code works great, as long as the number is under 1000. But, being that I am formatting the number as text, so that it has commas (ie: 1,234.56), whenever I try to run that first line, it errors out, saying: "fatal error: unexpectedly found nil while unwrapping an Optional value".
Any ideas what I can do to navigate around this issue?
But, being that I am formatting the number as text, so that it has commas (ie: 1,234.56)
You're trying to tackle the problem in the wrong way. If you're generating this string in the first place, then if you want to perform mathematical operations on the number, you shouldn't be displaying it as a string in the UI, then trying to go backwards from the UI back to a number. That's misusing your presentation layer as your data model.
Instead of trying to go back and forth between the UI, use the original value you generated the string from.
#Jim is correct; your text field should simply contain a textual representation of your internal variable, so you shouldn't need to convert back to a double, but for reference, you can use a NumberFormatter
import Foundation
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
if let number = formattter.number(from:"10,000") {
print(number.doubleValue)
}
I have made a calculator for simple operations but I cant figure out how should I add the operator signs next to the numerals that I am entering.
I created 2 functions 1 on the number being entered
-(IBAction)buttonDigitPressed:(id)sender
and another for the operation
-(IBAction)buttonOperationPressed:(id)sender.
calculatorScreen.text = [NSString stringWithFormat:#"%.2f",result];
This is for the result to be shown on the label calculatorScreen.
The result i would like would be something like "1+2*3/4" on the calculatorScreen.
Sorry if I misunderstand your question, but what you want is to display on your calculator app the full equation that you've input thus far (e.g. 63+42-62).
Like any other calculator, you should have 2 label, one for your current input, and one to show all that you've entered.(I'm guessing you need the latter)
With the second label up, you can add in the append function into your digitpressed, enter/= function, operation function. If you want to tweak it such 16+23-32 will show up as
1) 16+23
2) 39-32
3) 39-32=7
then you'll have to add in your own specific code. otherwise the label will input as 16+23-32 = 7
You can just append the character to whatever is already on calculatorScreen. Or you can save the current input in an instance variable and display where appropriate.
This is just a guideline, since I don't know the behavior of your calculator in case of this input: 1 + 2 * 3 (simple calculator will return 9, scientific will return 7).