CGFloat variable returns 0.0 [duplicate] - swift

This question already has an answer here:
Strange Swift numbers type casting
(1 answer)
Closed 5 years ago.
When I try this:
let progress: CGFloat = CGFloat(2 / 3)
print(progress)
The console returns 0 but the result is 0,66666666667, not 0.... Why is that happening?
When I try:
let progress: CGFloat = CGFloat(10 / 3)
print(progress)
It returns 3.0, is that the way how CGFloat works? Is there no way to get comma numbers with CGFLoat?

If you leave out converting, this will work perfectly.
let progress: CGFloat = 2 / 3
print(progress) //0.666666666666667
The reason why this does not work with explicit converting is beause Swift treats a whole number as an Int if it's without context.
That's exactly what is happening inside converting brackets.

Related

Swift 4.2 round function [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 4 years ago.
I have func in my app which trims numbers, e.x. 9.81->9.8. But after updating swift behavior has changed.
let myNum = 9.7
print(round(myNum / 0.1) * 0.1)
Swift 4.1.2 output - 9.7
Swift 4.2 output - 9.700000000000001
Please advise how to resolve this issue. May it is swift's bug?
If you need the correct value itself, and not just a String representation, you can try this.
let value = 9.71
let roundedValue = round(value * 10) / 10
print(roundedValue)
It prints 9.7 on Swift 4.2, so I guess that the rounded value is correct and you can use it for additional computations.
This seems to be what you want:
let trimmedString = String(format: "%#.1f", round(myNum / 0.1) * 0.1)

Swift-Binary operator cannot be applied to two "Double" operands [duplicate]

This question already has answers here:
binary operator '/' cannot be applied to two 'Double' operands
(3 answers)
Closed 6 years ago.
I'm receiving what appears to be a relatively common error in my code, but examples of solutions to it on other questions didn't appear to be quite relevant to my situation. For example; (binary operator '/' cannot be applied to two 'Double' operands)
let variable1 = Double()
let variable2 = Double()
let array = [5, 10]
var variable3 = Double()
func function() {
let variable1 = 50 / variable2
let variable3 = Double(arc4random_uniform(UInt32(Double(array.count))))
let scaleAction = SKAction.scale(by: variable1 * variable3, duration: 1)
That's all the relevant code anyway. For whatever reason I receive an error, focused on the multiplication star in the last line, saying that the "Binary operator "*" cannot be applied to two "Double" operands. Why can't it? And is there a way I can fix this?
Thanks in advance.
The error message is a bit misleading. The real problem is that scale(by:duration:) takes a CGFloat as the scale:
open class func scale(by scale: CGFloat, duration sec: TimeInterval) -> SKAction
So, you need to pass a CGFloat. You can either work to make sure variable1 and variable3 are CGFloats, or you can use the CGFloat constructor to convert it from a Double:
let scaleAction = SKAction.scale(by: CGFloat(variable1 * variable3), duration: 1)

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

binary operator / cannot be applied to operands of type Int and Double [duplicate]

This question already has answers here:
Multiplying variables and doubles in swift
(2 answers)
So if string is not NilLiteralConvertible... what do some string functions return?
(1 answer)
Closed 7 years ago.
Hello brand new to Swift, and programming in general. Going through an exercise the code given is exactly:
//: Playground - noun: a place where people can play
import UIKit
let height = 12
let width = 10
let area = height * width
let areaInMeters = area / 10.762
But I get the error, "binary operator / cannot be applied to operands of type Int and Double".
After some digging around I found you can't operate on both an Integer and a Double. So I changed the last line to:
let areaInMeters = (Double)area / 10.762
Then I get the error, "Consecutive statements on a line must be separated by a ;" and it wants me to put the ; after area. None of this is making any sense to me.
Using El Capitan beta and Xcode 7 beta.
height and width will both be inferred as of type Int. Therefore area is also of type Int whilst 10.762 is a Double.
And in Swift safety is paramount so you'll need to have both operands of same type.
Solution is (as Eric D. suggested) is to convert area to a Double:
let areaInMeters = Double(area) / 10.762
Try instead adding a decimal point and a zero to the end of your height and width.
Like so:
let height = 12.0
let width = 10.0
And you won't have to worry about having to deal with an Integer.
Hope this helps. Happy Coding!