How to round a Double to the nearest Int in swift?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to round a Double to the nearest Int in swift?



I'm trying to make a calculator of growth rate (Double) that will round the result to the nearest Integer and recalculate from there, as such:


Double


let firstUsers = 10.0
let growth = 0.1
var users = firstUsers
var week = 0


while users < 14
println("week (week) has (users) users")
users += users * growth
week += 1



but I've been unable so far.



EDIT
I kinda did it like so:


var firstUsers = 10.0
let growth = 0.1
var users:Int = Int(firstUsers)
var week = 0


while users <= 14
println("week (week) has (users) users")
firstUsers += firstUsers * growth
users = Int(firstUsers)
week += 1



Although I don't mind that it is always rounding down, I don't like it because firstUsers had to become a variable and change throughout the program (in order to make the next calculation), which I don't want it to happen.


firstUsers




6 Answers
6



There is a round available in the Foundation library (it's actually in Darwin, but Foundation imports Darwin and most of the time you'll want to use Foundation instead of using Darwin directly).


round


Foundation


Darwin


Foundation


Darwin


Foundation


Darwin


import Foundation

users = round(users)



Running your code in a playground and then calling:


print(round(users))



Outputs:



15.0



round() always rounds up when the decimal place is >= .5 and down when it's < .5 (standard rounding). You can use floor() to force rounding down, and ceil() to force rounding up.


round()


>= .5


< .5


floor()


ceil()



If you need to round to a specific place, then you multiply by pow(10.0, number of places), round, and then divide by pow(10, number of places):


pow(10.0, number of places)


round


pow(10, number of places)



Round to 2 decimal places:


let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let num = 10.12345
let rounded = round(num * multiplier) / multiplier
print(rounded)



Outputs:



10.12



Note: Due to the way floating point math works, rounded may not always be perfectly accurate. It's best to think of it more of an approximation of rounding. If you're doing this for display purposes, it's better to use string formatting to format the number rather than using math to round it.


rounded





Hmm pow() unfortunately not available in a playground
– MrBr
Nov 12 '15 at 10:33


pow()





@MrBr, pow() is defined in the Darwin library, so you need to import Darwin first (or import Foundation or import Cocoa or import UIKit, all of which end up importing Darwin internally).
– Mike S
Nov 12 '15 at 14:43


pow()


import Darwin


import Foundation


import Cocoa


import UIKit





There is also lround() which returns an Int.
– Martin R
Apr 2 '16 at 16:26


lround()


Int





"round() always rounds up when the decimal place is >= .5 and down when it's < .5 (standard rounding)." Except when it doesn't. round(-16.5) returns -17, not -16. Is this a bug?
– Daniel T.
Sep 8 '17 at 15:51


round()


round(-16.5)



To round a double to the nearest integer, just use round().


round()


var x = 3.7
x.round() // x = 4.0



If you don't want to modify the original value, then use rounded():


rounded()


let x = 3.7
let y = x.rounded() // y = 4.0. x = 3.7



As one might expect (or might not), a number like 3.5 is rounded up and a number like -3.5 is rounded down. If you need different rounding behavior than that, you can use one of the rounding rules. For example:


3.5


-3.5


var x = 3.7
x.round(.towardZero) // 3.0



If you need an actual Int then just cast it to one:


Int


let myInt = Int(myDouble.rounded())


round


lround


floor


ceil


CGFloat



Swift 3 & 4 - making use of the rounded(_:) method as blueprinted in the FloatingPoint protocol


rounded(_:)


FloatingPoint



The FloatingPoint protocol (to which e.g. Double and Float conforms) blueprints the rounded(_:) method


FloatingPoint


Double


Float


rounded(_:)


func rounded(_ rule: FloatingPointRoundingRule) -> Self



Where FloatingPointRoundingRule is an enum enumerating a number of different rounding rules:


FloatingPointRoundingRule



case awayFromZero


case awayFromZero



Round to the closest allowed value whose magnitude is greater than or
equal to that of the source.



case down


case down



Round to the closest allowed value that is less than or equal to the
source.



case toNearestOrAwayFromZero


case toNearestOrAwayFromZero



Round to the closest allowed value; if two values are equally close,
the one with greater magnitude is chosen.



case toNearestOrEven


case toNearestOrEven



Round to the closest allowed value; if two values are equally close,
the even one is chosen.



case towardZero


case towardZero



Round to the closest allowed value whose magnitude is less than or
equal to that of the source.



case up


case up



Round to the closest allowed value that is greater than or equal to
the source.



We make use of similar examples to the ones from @Suragch's excellent answer to show these different rounding options in practice.



.awayFromZero


.awayFromZero



Round to the closest allowed value whose magnitude is greater than or equal to that of the source; no direct equivalent among the C functions, as this uses, conditionally on sign of self, ceil or floor, for positive and negative values of self, respectively.


self


ceil


floor


self


3.000.rounded(.awayFromZero) // 3.0
3.001.rounded(.awayFromZero) // 4.0
3.999.rounded(.awayFromZero) // 4.0

(-3.000).rounded(.awayFromZero) // -3.0
(-3.001).rounded(.awayFromZero) // -4.0
(-3.999).rounded(.awayFromZero) // -4.0



.down


.down



Equivalent to the C floor function.


floor


3.000.rounded(.down) // 3.0
3.001.rounded(.down) // 3.0
3.999.rounded(.down) // 3.0

(-3.000).rounded(.down) // -3.0
(-3.001).rounded(.down) // -4.0
(-3.999).rounded(.down) // -4.0



.toNearestOrAwayFromZero


.toNearestOrAwayFromZero



Equivalent to the C round function.


round


3.000.rounded(.toNearestOrAwayFromZero) // 3.0
3.001.rounded(.toNearestOrAwayFromZero) // 3.0
3.499.rounded(.toNearestOrAwayFromZero) // 3.0
3.500.rounded(.toNearestOrAwayFromZero) // 4.0
3.999.rounded(.toNearestOrAwayFromZero) // 4.0

(-3.000).rounded(.toNearestOrAwayFromZero) // -3.0
(-3.001).rounded(.toNearestOrAwayFromZero) // -3.0
(-3.499).rounded(.toNearestOrAwayFromZero) // -3.0
(-3.500).rounded(.toNearestOrAwayFromZero) // -4.0
(-3.999).rounded(.toNearestOrAwayFromZero) // -4.0



This rounding rule can also be accessed using the zero argument rounded() method.


rounded()


3.000.rounded() // 3.0
// ...

(-3.000).rounded() // -3.0
// ...



.toNearestOrEven


.toNearestOrEven



Round to the closest allowed value; if two values are equally close, the even one is chosen; equivalent to the C rint (/very similar to nearbyint) function.


rint


nearbyint


3.499.rounded(.toNearestOrEven) // 3.0
3.500.rounded(.toNearestOrEven) // 4.0 (up to even)
3.501.rounded(.toNearestOrEven) // 4.0

4.499.rounded(.toNearestOrEven) // 4.0
4.500.rounded(.toNearestOrEven) // 4.0 (down to even)
4.501.rounded(.toNearestOrEven) // 4.0



.towardZero


.towardZero



Equivalent to the C trunc function.


trunc


3.000.rounded(.towardZero) // 3.0
3.001.rounded(.towardZero) // 3.0
3.999.rounded(.towardZero) // 3.0

(-3.000).rounded(.towardZero) // 3.0
(-3.001).rounded(.towardZero) // 3.0
(-3.999).rounded(.towardZero) // 3.0



If the purpose of the rounding is to prepare to work with an integer (e.g. using Int by FloatPoint initialization after rounding), we might simply make use of the fact that when initializing an Int using a Double (or Float etc), the decimal part will be truncated away.


Int


FloatPoint


Int


Double


Float


Int(3.000) // 3
Int(3.001) // 3
Int(3.999) // 3

Int(-3.000) // -3
Int(-3.001) // -3
Int(-3.999) // -3



.up


.up



Equivalent to the C ceil function.


ceil


3.000.rounded(.up) // 3.0
3.001.rounded(.up) // 4.0
3.999.rounded(.up) // 4.0

(-3.000).rounded(.up) // 3.0
(-3.001).rounded(.up) // 3.0
(-3.999).rounded(.up) // 3.0


FloatingPoint


FloatingPointRoundingRule



If we'd like, we can take a look at the source code for FloatingPoint protocol to directly see the C function equivalents to the public FloatingPointRoundingRule rules.


FloatingPoint


FloatingPointRoundingRule



From swift/stdlib/public/core/FloatingPoint.swift.gyb we see that the default implementation of the rounded(_:) method makes us of the mutating round(_:) method:


rounded(_:)


round(_:)


public func rounded(_ rule: FloatingPointRoundingRule) -> Self
var lhs = self
lhs.round(rule)
return lhs



From swift/stdlib/public/core/FloatingPointTypes.swift.gyb we find the default implementation of round(_:), in which the equivalence between the FloatingPointRoundingRule rules and the C rounding functions is apparent:


round(_:)


FloatingPointRoundingRule


public mutating func round(_ rule: FloatingPointRoundingRule)
switch rule
case .toNearestOrAwayFromZero:
_value = Builtin.int_round_FPIEEE$bits(_value)
case .toNearestOrEven:
_value = Builtin.int_rint_FPIEEE$bits(_value)
case .towardZero:
_value = Builtin.int_trunc_FPIEEE$bits(_value)
case .awayFromZero:
if sign == .minus
_value = Builtin.int_floor_FPIEEE$bits(_value)

else
_value = Builtin.int_ceil_FPIEEE$bits(_value)

case .up:
_value = Builtin.int_ceil_FPIEEE$bits(_value)
case .down:
_value = Builtin.int_floor_FPIEEE$bits(_value)






is still working in swift 4
– iosMentalist
Aug 16 at 11:49





@iosMentalist thanks for the prompt, I've update the title of answer.
– dfri
Aug 16 at 11:53




Swift 3:
If you want to round to a certain digit number e.g. 5.678434 -> 5.68 you can just combine the round() or roundf() function with a multiplication:


let value:Float = 5.678434
let roundedValue = roundf(value * 100) / 100
print(roundedValue) //5.68





Thank you! It's saved me.
– Raja
Sep 27 '17 at 12:41





Wow. So simple and so good. Thanks, mate.
– Felipe
Sep 5 at 20:24



Swift 3



var myNum = 8.09



myNum.rounded() //result = 8 which is stored in myNum





Nice. I didn't know about this one before. One note: myNum.rounded() does not change myNum, but myNum.round() does.
– Suragch
Dec 30 '16 at 13:01


myNum.rounded()


myNum


myNum.round()



You can also extend FloatingPoint in Swift 3 as follow:


extension FloatingPoint
func rounded(to n: Int) -> Self
return (self / Self(n)).rounded() * Self(n)




324.0.rounded(to: 5) // 325





Can you please explain this? What's Self means?
– JZAU
Jun 7 '17 at 7:20


Self





@Jacky Self refers to the class FloatingPoint whereas self refers to the instance of that class.
– George Yacoub
Aug 5 '17 at 18:21






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard