Round a double in Swift [duplicate] - swift

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

Related

Why is x / 100 == 0 when x != 0 in swift? [duplicate]

This question already has answers here:
Is the Swift divide "/" operator not working or have I missed something?
(3 answers)
Division not working properly in Swift
(3 answers)
Closed 1 year ago.
I have created a for loop in which I calculate a few values.
for i in 1...100{
let xValue = i/100
print(xValue) // returns 0 every time except when i == 100
}
This is a recreation of a part of that for loop. Why is it that I do not get the right value for 'xValue'?
For info I have also tried the following:
let xValue: Float = Float(i/100)
And that doesn't work either, despite me being very specific. I must have forgotten something basic about these arithmetic
operators in swift.
When you divide an Int by an Int, the result will be rounded down. Use a floating point type, like Double or Float for more precision.
for i in 1...100 {
let xValue = Float(i)/100
print(xValue)
}
To address your attempted solution - when you do:
let xValue: Float = Float(i/100)
The Int result is first computed in i/100 (and rounded down to 0) then you are casting to a Float.
Therefore, we cast i to a Float before the division so the result is computed as a Float.
Since i and 100 are both integer values, / will do integer division and the result will be truncated to 0.
Even when you do let xValue: Float = Float(i/100), the result of division inside the parentheses is already truncated to 0 before the value can be converted to a Float.
Convert i to a floating-point value before dividing to prevent the result from being truncated.
for i in 1...100{
let xValue = Float(i)/100
print(xValue)
}

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)

CGFloat variable returns 0.0 [duplicate]

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.

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 round double in swift to 2 places with math round [duplicate]

This question already has answers here:
Rounding a double value to x number of decimal places in swift
(34 answers)
Closed 7 years ago.
The standard swift's round() function just cuts off everything and leaves 1 place after the point.
extension Double {
// Rounds the double to decimal places value
// Usage: (doubleVar).roundToPlaces(x)
// Where x is the number of places
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
Source: Rounding a double value to x number of decimal places in swift