Rounding in Swift with round()

SwiftXcode6Rounding

Swift Problem Overview


While playing around, I found the round() function in swift. It can be used as below:

round(0.8)

Which will return 1, as expected. Here's my question:

how do I round by thousandths in swift?

I want to be able to plug in a number, say 0.6849, and get 0.685 back. How does round() do this? Or, does it not, in which case, what function does?

Swift Solutions


Solution 1 - Swift

You can do:

round(1000 * x) / 1000

Solution 2 - Swift

Updated answer

The round(someDecimal) is the old C style. As of Swift 3, doubles and floats have a built in Swift function.

var x = 0.8
x.round() // x is 1.0 (rounds x in place)

or

var x = 0.8
var y = x.rounded() // y is 1.0, x is 0.8

See my answer fuller answer here (or here) for more details about how different rounding rules can be used.

As other answers have noted, if you want to round to the thousandth, then multiply temporarily by 1000 before you round.

Solution 3 - Swift

func round(value: Float, decimalPlaces: UInt) {
  decimalValue = pow(10, decimalPlaces)
  round(value * decimalValue) / decimalValue
}

func round(value: CGFloat, decimalPlaces: UInt)
func round(value: Double, decimalPlaces: UInt)
func roundf(value: Float, decimalPlaces: UInt)

Solution 4 - Swift

Here's one way to do it. You could easily do this for Float, or probably make it generic so it's for any of those.

public extension CGFloat {
    func roundToDecimals(decimals: Int = 2) -> CGFloat {
        let multiplier = CGFloat(10^decimals)
        return round(multiplier * self) / multiplier
    }
}

Solution 5 - Swift

Swift 4:

(x/1000).rounded()*1000

Solution 6 - Swift

This will round to any value not limited by powers of 10.

extension Double {
    func roundToNearestValue(value: Double) -> Double {
        let remainder = self % value
        let shouldRoundUp = remainder >= value/2 ? true : false
        let multiple = floor(self / value)
        let returnValue = !shouldRoundUp ? value * multiple : value * multiple + value
        return returnValue
    }
}

Solution 7 - Swift

var a=1.2344 
var b:String = String(format: "%0.2f",a) 
print(b)

Output:

1.23

The output you got is of string type. So, you need to convert into other types if needed. The below link tells you how to convert:

https://supereasyapps.com/blog/2015/9/28/how-to-convert-strings-into-double-and-float-values-using-swift-2#:~:text=Convert%20Swift%20String%20to%20Double,(Double(%22200.0%22)!)

Solution 8 - Swift

Take a look at Apple's documentation for round() and rounded(_:).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrocket101View Question on Stackoverflow
Solution 1 - SwiftClément BisaillonView Answer on Stackoverflow
Solution 2 - SwiftSuragchView Answer on Stackoverflow
Solution 3 - SwiftmatrinoxView Answer on Stackoverflow
Solution 4 - SwiftDan RosenstarkView Answer on Stackoverflow
Solution 5 - SwiftHalyna RubashkoView Answer on Stackoverflow
Solution 6 - SwiftSethmrView Answer on Stackoverflow
Solution 7 - Swiftuser15057879View Answer on Stackoverflow
Solution 8 - Swiftuser12660318View Answer on Stackoverflow