How to use println in Swift to format number

前端 未结 4 1885
萌比男神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:16

    Everything about the format of a number as a string can be adjusted using a NSNumberFormatter:

    let nf = NSNumberFormatter()
    nf.numberStyle = NSNumberFormatterStyle.DecimalStyle
    nf.maximumFractionDigits = 2
    println(nf.stringFromNumber(0.33333)) // prints 0.33
    

    You can escape quotes with a backslash

    println("\"God is dead\" -Nietzsche")
    

提交回复
热议问题