Trying to understand a "C-style" statement in swift 3 [duplicate] - ios

This question already has answers here:
Swift 3 for loop with increment
(5 answers)
Replacement for C-style loop in Swift 2.2
(4 answers)
Express for loops in swift with dynamic range
(2 answers)
Closed 5 years ago.
I am self-learning to program and I came across this piece of Swift 2.0 code.
someFunction {
for var i = N; i >= 1; i -= 1 {
//...
}
}
This is a "C-Style" code apparently. What exactly is happening in this control flow? Are we starting from N, and subtracting 1 until we get to equal/greater than 1?
Or does the i >= 1 mean that the iteration count must ALWAYS be greater than or equal to one?

Related

Check if integer divided by 2. SWIFT [duplicate]

This question already has answers here:
How to know if a number is odd or even in Swift?
(5 answers)
Closed 11 months ago.
im absolutely new to development, and trying to learn swift.
Right now i know how to make random number, and my next step is:
Im trying to understand how to check if my random number (127) could be divided by 2 without decimals ?
I have no idea how to do it.
There is a specific API isMultiple(of:) in Standard Library for this purpose
let random = Int.random(in: 0..<100)
let isEven = random.isMultiple(of: 2)
You can use operator % - remainder operator
example:
if randomNumber % 2 == 0 {
print("\(randomNumber) is even")
} else {
print("\(randomNumber) is odd")
}

convert to currency using Swift [duplicate]

This question already has answers here:
How to properly format currency on ios
(8 answers)
How to input currency format on a text field (from right to left) using Swift?
(9 answers)
Closed 4 years ago.
I have a values like that: 100, 1220, 10015 basically last 2 digits are cents and I need to convert to dollar (currency) format similar to:
1.00, 12.20, 100.15
Can somebody suggest a quick implementation?
var a = 1011
var b = Double(a) / 100

How we can write for loop syntax in swift 3.0 [duplicate]

This question already has answers here:
#warning: C-style for statement is deprecated and will be removed in a future version of Swift [duplicate]
(4 answers)
Fix warning "C-style for Statement is deprecated" in Swift 3
(4 answers)
Closed 6 years ago.
How we can differentiate for loop and for each loop in swift language ?
do like
for i in 0..<100 {
print("Number \(i)")
}
for more sample, you can get here
If you want to do it with some step, just use it (step is 2):
for i in stride(from: 0, to: 100, by: 2) {
print(i)
}

Rails round number [duplicate]

This question already has answers here:
A way to round Floats down
(11 answers)
Closed 6 years ago.
I have number num = 24.89808 and want to round it to 24.89
How can i do it?
i tried num.round(2) but it gives me 24.9 also number_to_currency=>24.90
If you are talking about rounding, 24.9 is by all means the correct result. Whether you are interested in ceiling it, here you go:
(24.89808 * 100).floor / 100.0
#⇒ "24.89"
First convert that into a decimal and then round it two two places,
Try this command,
num.to_d.round(2, :truncate).to_f
2.2.4 :040 > num = 24.89808
=> 24.89808
2.2.4 :041 > num.to_d.round(2,:truncate).to_f
=> 24.89

Generating random number 1 - 100 in Swift 2 [duplicate]

This question already has answers here:
How to generate a random number in Swift?
(26 answers)
Closed 7 years ago.
I was wondering if this is the best way to generate a random number in Swift.
This is what I have thought of thus far:
var randomNumber:Int = random() * 100 + 1 //What value does this return?
I was wondering if this is a viable 1 - 100 range in Swift utilizing the random() function? I am unsure if this is. I have not dealt with random numbers much in Swift 2. However in Java, the equivalent would be Math.random() * 100 + 1
I'm curious to know what would be the equivalent of this in Swift 2. Thanks in advance!
let random = Int(arc4random_uniform(100) + 1)
+ 1 since arc4random_uniform generates a random number between 0 and the parameter - 1

Resources