51

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?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
rocket101
  • 7,369
  • 11
  • 45
  • 64

8 Answers8

93

You can do:

round(1000 * x) / 1000
Clément Bisaillon
  • 5,037
  • 8
  • 32
  • 53
21

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.

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
11
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)
Axe
  • 6,285
  • 3
  • 31
  • 38
matrinox
  • 472
  • 5
  • 8
  • ` static func round_pow_2( value: CGFloat, decimalPlaces: CGFloat ) -> CGFloat { let decimalValue = pow( 2.0, decimalPlaces ) return round( value * decimalValue ) / decimalValue }` – Bobjt Feb 29 '16 at 22:00
  • Could be just me but that function doesn't make much sense? – Mr Dog Dec 09 '16 at 04:17
4

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
    }
}
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
  • 10^decimal does a bitwise XOR. It should be replaced with pow or powf should be used instead. – Yogurt May 20 '18 at 23:58
4

Swift 4:

(x/1000).rounded()*1000
Halyna Rubashko
  • 605
  • 5
  • 7
  • 1
    This will give 0 if `x` is 0.6849 as given in original question. Should be `(x*1000).rounded()/1000` – yjsoon Jan 27 '20 at 15:34
3

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
    }
}
Sethmr
  • 3,046
  • 1
  • 24
  • 42
2
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)!)

  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted](//stackoverflow.com/help/deleted-answers). – Sabito stands with Ukraine Jan 22 '21 at 07:58
1

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