SWIFT中的精确字符串格式规范下面是我以前如何将浮点数截断为小数点后两位NSLog(@" %.02f %.02f %.02f", r, g, b);我检查了文档和电子书,但还没有弄清楚。谢谢!
3 回答
慕斯709654
TA贡献1840条经验 获得超5个赞
import Foundationextension Int { func format(f: String) -> String { return String(format: "%\(f)d", self) }}extension Double { func format(f: String) -> String { return String(format: "%\(f)f", self) }}let someInt = 4, someIntFormat = "03"println("The integer number \(someInt) formatted with \"\(someIntFormat)\" looks like \(someInt.format(someIntFormat))")// The integer number 4 formatted with "03" looks like 004let someDouble = 3.14159265359, someDoubleFormat = ".3"println("The floating point number \(someDouble) formatted with \"\(someDoubleFormat)\" looks like \(someDouble.format(someDoubleFormat))")// The floating point number 3.14159265359 formatted with ".3" looks like 3.142
阿晨1998
TA贡献2037条经验 获得超6个赞
String.localizedStringWithFormat
let value: Float = 0.33333let unit: String = "mph"yourUILabel.text = String.localizedStringWithFormat("%.2f %@", value, unit)
- 3 回答
- 0 关注
- 581 浏览
添加回答
举报
0/150
提交
取消