How to use println in Swift to format number

前端 未结 4 1884
萌比男神i
萌比男神i 2020-12-28 14:56

When logging-out a float in Objective-C you can do the following to limit your output to only 2 decimal places:

float avgTemp = 66.844322156
NSLog (@\"averag         


        
4条回答
  •  感动是毒
    2020-12-28 15:26

    Here's the shortest solution I found thus far:

    let avgTemp = 66.844322156
    println(NSString(format:"%.2f", avgTemp))
    

    Its like the swift version of NSString's stringWithFormat

提交回复
热议问题