how to convert hour to seconds in swift? [duplicate] - swift

This question already has answers here:
Swift - Integer conversion to Hours/Minutes/Seconds
(25 answers)
Closed 3 months ago.
I need to convert this code to an hours
func convert_to_seconds(hours: Int) -> Int {
// write your code here
}
the input is:
hours = 6
the output is:
21600
I tried several codes but they didn't work for me

func convert_to_seconds(hours: Int) -> Int {
hours*3600
}

Related

scala: any built-in function to divide a double by another double and return quotient and remainder as a tuple [duplicate]

This question already has answers here:
Is there a division operation that produces both quotient and reminder?
(4 answers)
Closed 3 years ago.
I can use the following function but is there a better option?
def div(dur:Double) = {
var days:Int = dur.toInt/24
var hours:Double = dur%24
(days, hours)
}
works since scala 2.8
import scala.math.Integral.Implicits._
val (days, hours) = dur.toInt /% 24

How to stop rounding in NSDecimalNumber? [duplicate]

This question already has answers here:
In Swift 3, how to calculate the factorial when the result becomes too high?
(2 answers)
BigInteger equivalent in Swift?
(6 answers)
Closed 3 years ago.
I am solving a question from HackerRank which asks to print the value of extra-long factorials that can't be stored even in a 64-bit long variable.
I am using NSDecimalNumber to store the value. However, even in this case, the final result is rounded off.
func extraLongFactorials(n: Int) -> Void
{
var factorial: NSDecimalNumber = 1
for index in 1...n
{
let indexInNSDecimal = NSDecimalNumber(value: index)
factorial = factorial.multiplying(by: indexInNSDecimal)
}
let factorialWithoutRounding = factorial.description(withLocale: nil)
print(factorialWithoutRounding)
}
print(extraLongFactorials(n: 45)) // 119622220865480194561963161495657715064000000000000000000
However, the result should be 119622220865480194561963161495657715064383733760000000000.
This link talks about using description(withLocale:).
NSDecimalNumber round long numbers
However, it does not clearly explain how to use the description(withLocale:) method.
I also went through the apple doc https://developer.apple.com/documentation/foundation/nsdecimalnumber/1412789-description. But it also does not explain clearly how to use it.
Can someone please discuss this method in detail.

Swift 3 - Make a Random number generate in between 2 integer variables [duplicate]

This question already has answers here:
How does one make random number between range for arc4random_uniform()?
(18 answers)
Closed 5 years ago.
i'm semi new to xcode and swift, and i'm just making some small apps to play around with some stuff. I wanted to try and make a number generator in which you set the minimum number and maximum number and the app will pick a random number in between the two. Would this be possible?
I all that I need help with is making a random number appear in between two variable integers just to clarify.
Thanks
Swift 4.2 Edit:
func randomBetween(min: Int, max: Int) -> Int {
return Int.random(in: min ..< max)
}
import GameKit
func randomBetween(min: Int, max: Int) -> Int {
return GKRandomSource.sharedRandom().nextInt(upperBound: max - min) + min
}

Xcode Swift 3 ~ Make speed into an integer [duplicate]

This question already has answers here:
Convert Float to Int in Swift
(15 answers)
Closed 5 years ago.
I understand that Int() is how to make something and integer but this doesn't work when using it with speed. Here is my code:
var speed: CLLocationSpeed = CLLocationSpeed()
speed = location.speed * 2.23694
if location.speed < 1 {
speedLabel.text = "0"
}
else {
speedLabel.text = "\(speed)"
}
Just wondering how to make my speed (mph) into an integer because currently I am getting 2 decimal points which make my app look messy. Thanks in advance
Use a rounding function. In action:
22> round (1.6)
$R10: Double = 2
23> Int(round (1.6))
$R11: Int = 2

How to generate random numbers without repetition in Swift? [duplicate]

This question already has answers here:
Unique (non-repeating) random numbers in O(1)?
(22 answers)
How do I shuffle an array in Swift?
(25 answers)
Closed 8 years ago.
How can I generate random numbers between 0 and 31 without call the number twice in Swift Language ?
You can create a function that returns a random-number generating closure, like this:
func randomSequenceGenerator(min: Int, max: Int) -> () -> Int {
var numbers: [Int] = []
return {
if numbers.isEmpty {
numbers = Array(min ... max)
}
let index = Int(arc4random_uniform(UInt32(numbers.count)))
return numbers.remove(at: index)
}
}
To use it, first call the generator once to create the random sequence generator, then call the return value as many times as you like. This will print out random values between one and six, cycling through all of them before starting over:
let getRandom = randomSequenceGenerator(min: 1, max: 6)
for _ in 1...20 {
print(getRandom())
}