ios - Swift - How to remove a decimal from a float if the decimal is equal to 0? -
i'm displaying distance 1 decimal, , remove decimal in case equal 0 (ex: 1200.0km), how in swift? i'm displaying number this:
let distancefloat: float = (currentuser.distance! nsstring).floatvalue distancelabel.text = string(format: "%.1f", distancefloat) + "km"
how this?
let distancefloat: float = (currentuser.distance! nsstring).floatvalue distancelabel.text = string(format: distancefloat == floor(distancefloat) ? “%.0f" : "%.1f", distancefloat) + "km" or extension:
extension float { var clean: string { return self % 1 == 0 ? string(format: "%.0f", self) : string(self) } }
Comments
Post a Comment