Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I's sorry but I'm new to Xcode and coding.
Im trying to create an If statement so that if label 3 = or greater then 20 label 4 = 0, else if lower then 20 it = 1 and if lower then 18.5 its = 2.
this is my code for that:
if (_label3.text >= #"20") {_label4.text = #"0";}
else if (_label3.text < #"20") {_label4.text = #"1";}
else if (_label3.text <= #"18.5") {_label4.text = #"2";}
I'm not sure what is going wrong, but i am getting this error 'Direct comparison of String literal has undefined behavior' and Xcode wont let me build the app.
Thank for your Help
you are using arithmetic operations on strings. that for sure makes no sense
create a float from the textfield input
if ([_label3.text floatValue] >= 20.0) {_label4.text = #"0";}
aslo you have to change the 1st and 2nd else branch, a the last one will never be called, as if it is true, the fist one is also be true.
float value = [_label3.text floatValue];
if (value > 20.0) {_label4.text = #"0";}
else if (value <= 18.5) {_label4.text = #"2";}
else if (value < 20.0) {_label4.text = #"1";}
label.text is string value and you cant compare string to int. Change the string to int and then compare like this
if([_label3.text intValue] >= 20){
_label4.text = #"0";
}
Hope this helps.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
What I want.
A: $1.10 = $1.1
B: $0.10101010101 = $0.101010
What I've tried
Solution one -
String(format: "%.6f", price)
This will do the following:
A: $1.100000
B: $0.1010101
So B is gets the correct outcome but A gets more decimals.
Solution two -
numberFormatter.numberStyle = .decimal
This gives the following outcome
A: $1.1
B: $0.1
So here A is correct but B gets rounded up.
This code will work for removing trailing zeros
let distanceFloat1: Float = 1.10
let distanceFloat2: Float = 0.10101010101
print("Value \(distanceFloat1.clean)")
print("Value \(distanceFloat2.clean)")
extension Float {
var clean: String {
return self.truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}
}
Output
Value 1.1
Value 1.101010
if you want to remove trailing zeros, also want to remove decimals after x place use
Swift - Remove Trailing Zeros From Double
I removed the trailing zeros using Ben's answer.
var stringWithoutZeroFraction: String {
return truncatingRemainder(dividingBy: 1) == 0 ? String(format: "%.0f", self) : String(self)
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
i have double value 70514.94971385633, now I want it to round the value and make it 70515.00. I have tried command rounded(), but it only rounds value after the decimal. How I can round value after the decimal and add the nearest value to a number before decimal? this is my code for rounding the value,
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded()
but it shows result 70514.95, i want it to add it before decimal value like 70515.00
Just small change in your code
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.round()
or
let roundedValue = totalBidValue.rounded(.up)
Use ceil(_:) to get that working,
let value = 70514.94971385633
let result = ceil(value)
print(result) //70515.0
Use round()
let myDoubleValue = 70514.94971385633
let roundedOffValue = round(myDoubleValue)
print(roundedOffValue) // 70515.0
Your code is almost perfect...
let totalBidValue = self.minBid / usdToAED!
let roundedValue = totalBidValue.rounded(.toNearestOrAwayFromZero)
just add .toNearestOrAwayFromZero in your code
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
var num = "100"
var num = text2.text.toInt()
var temp = 0
var temp2 = 1
if (nu == 1) {
println(1)
}
else {
for var valued = 2; valued<num;++valued {
var temp3 = temp + temp2
temp = temp2
temp2 = temp3
println("\(temp3)")
}
I want to print the fibonacci series of number. The number should be any one that should be choose by user. My code is above i have to choose num as 100 but it will print up to 47 values. The final value print here is 1836311903. It will not print up to 100. It shows an error. How i find the fibonacci series for number 100.
Fib(47) is 2,971,215,073.
2,971,215,073 is larger than 231 - 1 ... which is the largest 32 bit signed integer.
Hence your calculation is overflowing. (In a lot of programming languages, you wouldn't have gotten an error. You would have just gotten the wrong answer.)
How i find the fibonacci series for number 100.
You can't use simple integer arithmetic. You will need to use the Swift equivalent of Java's BigInteger class.
BigInteger equivalent in Swift?
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This is the function:
for (i = 0; i <= array.Length; i++) {
if (array[i].transform.position = 0)
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
else if (array[i].transform.position > 0)
array[i].transform.forward = Vector3(1,0,0);
}
I'm not sure if this is a valid question but there is for sure some logic errors :
First of all i'm not sure there is a RemoveAt(int index) for arrays
(but i'm not a big unityscript user) (there is for List though)
You should absolutely never (even if some weird languages maybe allow it to you) try to access an object you just deleted... which is what you try to do here :
array.RemoveAt(i);
print(“Removed element: “ + array[i].name);
Position is a Vector3 NOT a int or float so you cannot do : array[i].transform.position = 0
You should never use the = (assignment operator) in an if() you should use the == (comparison operator) (because = returns always true when assignment is possible)
That line is wrong for the same reason as before array[i].transform.position > 0
array[i].transform.forward = Vector3(1,0,0); Leaves me wondering because if it was C# i'd try the new keyword before Vector3() and i'd prefer floats that way :
array[i].transform.forward = new Vector3(1.0F,0,0); But even there Unity will throw you an error stating that you cannot modify components of Transform without making a copy first i believe...
But nice try :D
you can't compare a vector to 0 it needs to be like this
if(myObject.transform.position==Vector3.zero)
and for removing things I'd suggest to include System.Collections.generic lib
then use Listarray then you can use array.RemoveAt(index);
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In a turn-based game, I'd like to find the previous player.
To find the next player, I can just type:
int lastPlayer = match.currentPlayer - 1;
The problem is when the currentPlayer is player 1. Then lastPlayer becomes 0, which is wrong. It should be player6.
To fix this, I can do:
int lastPlayer = match.currentPlayer - 1;
if (lastPlayer == 0)
lastPlayer = match.numberOfPlayers;
My question is how to write this in a cleaner way. I know game center, turn based code do something like:
(currentIndex + 1) % match.participants.count];
How can I rewrite my code to do the same?
Thanks in advance
How about that?
int lastPlayer = match.currentPlayer > 1 ? match.currentPlayer - 1 : match.numberOfPlayers;
If you have a turn counter like this:
for(int i=0;i< maxTurns;i++){//do something}
and player1 plays the 1st turn, player2 the second and so on,
you can get the current players number exactly as its done in gamecenter.
Maybe you don't know the modulo "%" operator,
it returns the rest of a division, for example
3%2 = 1
your turncounter keeps counting up so the next player is
(turncounter + 1)%match.numberOfPlayer
in this case the modulo operator kind of "resets" your counter when it reaches the max player count (its round 6 and there are 5 players 6%5 = 1).
To sum it up, currentPlayer is turncounter%match.numberOfPlayer
nextPlayer is ++turncounter%match.numberOfPlayer