1

I am making a calculation to find a value of a double (below)

let tipAmt = Double(billAmt! * tipPer)

However, I want to take this value and round it up to the closest integer. How would I do that. Is there a round call?

1 Answers1

3

There is, literally a round() method that works on Double

let billAmt: Double? = 23.75
let tipPer: Double = 0.15

let tipAmt = Double(billAmt! * tipPer)

print("tipAmt: \(tipAmt)") // 3.5625

var rounded = round(tipAmt)
print("rounded to nearest dollar: \(rounded)") // 4

rounded = round(tipAmt * 100) / 100
print("rounded to nearest cent: \(rounded)") // 3.56
Dave Wood
  • 13,143
  • 2
  • 59
  • 67