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
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 .
To round a double to the nearest integer, just use round() .
Try:
var numberOfBottles = totalVolume / volumeEachBottles numberOfBottles.rounded(.up)
or
numberOfBottles.rounded(.down)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With