简体   繁体   中英

Convert a float into a string in Swift iOS

I want to convert a Float into a string.

myFloat: Float = 99.0
myString: String

How would I convert myFloat into a string so I can assign the following code

myString = myFloat

Similar to Connor's answer, you can do the following to have a little more control about how your Double or Float is dislpayed...

let myStringToTwoDecimals = String(format:"%.2f", myFloat)

This is basically like stringWithFormat in objC.

You can use String Interpolation :

var myFloat: Float = 99.0
var myString: String = "\(myFloat)"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM