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

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -