Swift: Trunc a floating number to show it in a label [duplicate] - swift

This question already has answers here:
String formatting of a Double [duplicate]
(2 answers)
Closed 8 years ago.
When I wanted to trunc a floating number for showing in a label in Objective-C I could just use LABEL.text = [NSString stringWithFormat:#"%.3f", FLOAT];
Though I can't find how to do so in Swift.

The old NSString format is still available. You can use that.
let myfloat : Float = 1.23
LABEL.text = NSString(format: "%.3f", myfloat)

Related

With Swift How Can I Convert String To Double one-to-one [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
swift: issue in converting string to double
(2 answers)
Swift double to string
(16 answers)
Closed 22 days ago.
While converting the string to double, I cannot do exactly the same. how can i fix this
StringValue = "0.00001174"...DoubleValue = Double(StringValue)...
print(DoubleValue) ->"0.000011749999999999999"
How can I get same . Thanks

How can i remove all the numbers from a string in Swift 3? [duplicate]

This question already has an answer here:
How to remove numbers from a String in Swift
(1 answer)
Closed 5 years ago.
I need to remove all the numbers from a string, but the code that I have is in swift 2.3
let string = (string.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet()) as NSArray).componentsJoinedByString("")
How can I do the same in swift 3? thanks!
Try this code
let string = string.components(separatedBy: CharacterSet.decimalDigits).joined()

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

Convert a CGPointo to Int in swift [duplicate]

This question already has answers here:
Convert Float to Int in Swift
(15 answers)
Closed 7 years ago.
I'm building a class to handle with a basic graphic. I'm a newby and I'm on a self learning project to build small apps in Swift for IOS devices, since I found the language very easy to learn.
I've set in my class of type UIView two constants of type CGPoint:
let heigh = bounds.size.heigh
let width = bounds.size.width
These 2 variables are CGFloat, but I need them to be Int.
Appreciate help to convert them to these type value.
You should be able to convert the float value to an Integer like this:
let height = Int(bounds.size.height)