2016-05-20 11 views
1

ich eine Berechnung machte einen Wert ein Doppel (unten)Runde Doppel in Swift

let tipAmt = Double(billAmt! * tipPer) 

jedoch zu finden, die ich diesen Wert nehmen will und um es auf die nächste ganze Zahl auf. Wie würde ich das tun? Gibt es einen Rundruf?

+3

auf dort arbeitet ein runder Anruf ist, sehen diese andere [Antwort] (http://stackoverflow.com/questions/25513357/rounding-in- rapid-with-round) – Scriptable

+0

* "Gibt es einen Rundruf?" * - Versuchen Sie 'man round' in einem Terminal-Fenster! –

Antwort

3

Es gibt buchstäblich ein round() Methode, die 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