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)
Related
This question already has answers here:
Math divison in Swift
(4 answers)
Closed 2 years ago.
I am facing a problem where I am not getting values of decimal places here is the code that I used to run in Swift Playground
print(100/1000)
print(Float(100/1000))
Expected Output :
0.10
0.10
Actual Output:
0
0.0
Because of this:
let variable = 100/1000
print(type(of: variable))
// prints Int
Just do the following:
print(100.0 / 1000.0)
print(Float(100) / Float(1000))
So in order to get a floating point result you need to divide floating point numbers instead of two integers
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.
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
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
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!