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

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

Related

Converting Integer to Character in Swift [duplicate]

This question already has answers here:
How to convert an Int to a Character in Swift
(9 answers)
Closed 2 years ago.
How can i convert an integer say 0 to a , 1 to b , 2 to c in Swift . I know in C++ and Python but could not find a straight forward way in swift
C++ code below
char a = 0 + 97;
char b = 1 + 97;
You can achieve the same thing in Swift like this:
let a = Character(UnicodeScalar(0 + 97))
let b = Character(UnicodeScalar(1 + 97))

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.

How do I convert Float to Int When Necessary? [duplicate]

This question already has answers here:
Swift - How to remove a decimal from a float if the decimal is equal to 0?
(15 answers)
Closed 6 years ago.
So, I'm pretty new to swift and Xcode and might be missing something obvious, but I've done a bit of research, and can't find my answer.
My code is:
for number in currentList {
listPreview.text = "\(listPreview.text!) \(String(number))"
}
The problem is, the Numbers in currentList are Floats. But if my user were to input a number that isn't a float, it will display as:
"UsersNumber".0
I want it to display as just:
"UsersNumber"
However, if the number the user gave me was a float, say... 1.2, I would still want it to display as 1.2 .
is there some kind of extension that can do this?
Like an if-statement saying
if number.isUselessFloat {
code
}
Thanks in advance,
-Another Nooby user
let number1 = 1.0
let number2 = 1.2
let str = String(format: number1 == floor(number1) ? "%.0f":"%.1f", number1)
print(str)
//prints 1
let str2 = String(format: number2 == floor(number2) ? "%.0f":"%.1f", number2)
print(str2)
//prints 1.2

Round a double in Swift [duplicate]

This question already has answers here:
Rounding in Swift with round()
(8 answers)
Closed 6 years ago.
I am making a calculation to find a value of a double (below)
let tipAmt = Double(billAmt! * tipPer)
However, I want to take this value and round it up to the closest integer. How would I do that. Is there a round call?
There is, literally a round() method that works on Double
let billAmt: Double? = 23.75
let tipPer: Double = 0.15
let tipAmt = Double(billAmt! * tipPer)
print("tipAmt: \(tipAmt)") // 3.5625
var rounded = round(tipAmt)
print("rounded to nearest dollar: \(rounded)") // 4
rounded = round(tipAmt * 100) / 100
print("rounded to nearest cent: \(rounded)") // 3.56

Is there an easier way to get the lesser of two values in Swift [duplicate]

This question already has answers here:
Swift equivalent for MIN and MAX macros
(5 answers)
Closed 8 years ago.
I'd like to assign the lesser of two values to a variable. In Ruby I would do something like:
my_var = [value_one, value_two].min
In Swift, of course, I can do this:
var myVar = 0.0
if valueOne < valueTwo {
myVar = valueOne
} else {
myVar = valueTwo
}
But, I'm wondering if there is a cleaner, more succinct solution.
var myVar = min(valueOne, valueTwo)
min is a standard library function that takes the lesser of two (or least of several — it's variadic) Comparable values.