Format float value with 2 decimal places [duplicate]

You can use standard string formatting specifiers to round to an arbitrary number of decimal places. Specifically %.nf where n is the number of decimal places you require:

let twoDecimalPlaces = String(format: "%.2f", 10.426123)

Assuming you want to display the number on each of the l* labels:

@IBAction func Berechnen(sender: AnyObject) {

    var Zahl = (txt.text as NSString).floatValue

    l5.text  = String(format: "%.2f", (Zahl / 95) * 100)
    l10.text = String(format: "%.2f", (Zahl / 90) * 100)
    l15.text = String(format: "%.2f", (Zahl / 85) * 100)
    l20.text = String(format: "%.2f", (Zahl / 80) * 100)
    l25.text = String(format: "%.2f", (Zahl / 75) * 100)
    l30.text = String(format: "%.2f", (Zahl / 70) * 100)
    l35.text = String(format: "%.2f", (Zahl / 65) * 100)
    l40.text = String(format: "%.2f", (Zahl / 60) * 100)
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)