Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Rounding up Double

I am trying to round a double up to a whole number,

var numberOfBottles = totalVolume / volumeEachBottles 

for example numberOfBottles = 275.0 / 250.0 that would give me 1.1, I need it to round up to 2

like image 811
Luis Gustavo Origa Avatar asked Jul 04 '15 21:07

Luis Gustavo Origa


People also ask

How do you round a double in Swift?

We can round a double to the nearest Int by using the round() method in Swift. If the decimal value is >=. 5 then it rounds up to the nearest value. for example, it rounds the 15.7 to 16.0 .

How do you round double to nearest Int in Swift?

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


1 Answers

Try:

var numberOfBottles = totalVolume / volumeEachBottles numberOfBottles.rounded(.up)  

or

numberOfBottles.rounded(.down) 
like image 102
João Nunes Avatar answered Sep 21 '22 12:09

João Nunes