How to round a Double to the nearest Int in swift?
Asked Answered
F

9

207

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:

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.

Friedland answered 14/10, 2014 at 0:35 Comment(0)
A
300

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).

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.

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):

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.

Attorneyatlaw answered 14/10, 2014 at 1:2 Comment(5)
Hmm pow() unfortunately not available in a playgroundLibnah
@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).Attorneyatlaw
There is also lround() which returns an Int.Drislane
"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?Spindly
@DanielT. - not a bug. It's rounding up to the nearest larger negative number. Think about it this way, +16.5 to +17 is moving 0.5 further away from zero. That means -16.5 to -17 is also 0.5 further away from zero. Ceil would be the opposite, +16.5 to +16 is 0.5 closer to zero and -16.5 to -16 is also 0.5 closer to zeroAjax
H
169

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

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

If you don't want to modify the original value, then use 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:

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

If you need an actual Int then just cast it to one (but only if you are certain that the Double won't be greater than Int.max):

let myInt = Int(myDouble.rounded())

Notes

  • This answer is completely rewritten. My old answer dealt with the C math functions like round, lround, floor, and ceil. However, now that Swift has this functionality built in, I can no longer recommend using those functions. Thanks to @dfri for pointing this out to me. Check out @dfri's excellent answer here. I also did something similar for rounding a CGFloat.
Heelpost answered 14/1, 2016 at 9:3 Comment(5)
Int(myDouble.rounded()) <--- this might actually throw an exception if the double doesn't fit the IntKemper
@Toad, are you sure? I don't see that in the documentation.Heelpost
I just solved a crash in production with this exact issue. But even if I was wrong and it wouldn't crash, then still it would still give unexpected results for doubles > maxintKemper
@Toad, right, good point, thanks. I added a note to the answer.Heelpost
lround() kind of feels nicer than Int(myDouble.rounded())Cuttler
C
135

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

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

func rounded(_ rule: FloatingPointRoundingRule) -> Self

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

case awayFromZero

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

case down

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

case toNearestOrAwayFromZero

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

case toNearestOrEven

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

case towardZero

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

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

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.

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

Equivalent to the C floor function.

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

Equivalent to the C round function.

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.

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

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

.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.

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) // 5.0 (up to nearest)

.towardZero

Equivalent to the C trunc function.

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(3.000) // 3
Int(3.001) // 3
Int(3.999) // 3

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

.up

Equivalent to the C ceil function.

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

Addendum: visiting the source code for FloatingPoint to verify the C functions equivalence to the different FloatingPointRoundingRule rules

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.

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:

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:

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)
    }
}
Casares answered 5/1, 2017 at 15:9 Comment(2)
@iosMentalist thanks for the prompt, I've update the title of answer.Casares
If I want any equation like, 3.0 = 3, 3.1 = 3.5, 3.4 = 3.5, 3.6 = 4, 3.9 - 4Bausch
G
7
**In Swift**

var a = 14.123456789
var b = 14.123456789
var c = 14.123456789
var d = 14.123456789
var e = 14.123456789
var f = 14.123456789

a.rounded(.up)                      //15
b.rounded(.down)                    //14
c.rounded(.awayFromZero)            //15
d.rounded(.towardZero)              //14
e.rounded(.toNearestOrAwayFromZero) //14
f.rounded(.toNearestOrEven)         //14
Gaye answered 26/9, 2018 at 5:15 Comment(0)
P
6

You can also extend FloatingPoint in Swift 3 as follow:

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

    }
}

324.0.rounded(to: 5)   // 325
Proconsulate answered 14/1, 2017 at 2:34 Comment(3)
Can you please explain this? What's Self means?Hennessy
@Jacky Self refers to the class FloatingPoint whereas self refers to the instance of that class.Gannet
@GeorgeYacoub Self refers to the type that conforms to FloatingPoint that’s being extended (in that sample usage is a Double) but they are structures, not classesProconsulate
G
6

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
Gottschalk answered 20/9, 2017 at 13:40 Comment(0)
A
4

Swift 3

var myNum = 8.09
myNum.rounded() // result = 8 and leaves myNum unmodified
Antecedency answered 29/12, 2016 at 15:43 Comment(2)
Nice. I didn't know about this one before. One note: myNum.rounded() does not change myNum, but myNum.round() does.Heelpost
@Suragch, I've edited the answer to reflect your comment.Ocampo
S
-1

A very easy solution worked for me:

  if (62 % 50 != 0) {
      var number = 62 / 50 + 1 // adding 1 is doing the actual "round up"
  }

number contains value 2

Schoolmistress answered 27/11, 2019 at 19:36 Comment(2)
Use floatValue.rounded() default function.Team
This isn't really addressing the question at all since there is no usage of floating point numbers of any variety, all calculations are done using Int. It will also yield incorrect values for many cases depending on your expectation of "round" (i.e., 62.0/50.0 rounded should be 1.0). This is closest to "ceiling", ie., the next integral value greater than given value. But even that isn't correct as the ceiling of 50.0/50.0 is 1.0, but your method will yield 2.0.Analemma
S
-2

This is the easiest way I came across

let weightValue = 143.69
print("Rounded off weight value = \(Int(weightValue))")

Alternate method:

print("Rounded off weight value = \((String(format: "%.0f", sender.value)))")
Stadtholder answered 14/2, 2022 at 6:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.