Kali Aney Kali Aney - 3 months ago 66
iOS Question

Swift - How to remove a decimal from a float if the decimal is equal to 0?

I'm displaying a distance with one decimal, and I would like to remove this decimal in case it is equal to 0 (ex: 1200.0Km), how could I do that in swift?
I'm displaying this number like this:

let distanceFloat: Float = (currentUser.distance! as NSString).floatValue
distanceLabel.text = String(format: "%.1f", distanceFloat) + "Km"

Answer

How about this?

    let distanceFloat: Float = (currentUser.distance! as NSString).floatValue
    distanceLabel.text = String(format: distanceFloat == floor(distanceFloat) ? "%.0f" : "%.1f", distanceFloat) + "Km"
Comments