具有 Float 类型的 Swift String 初始值设定项生成奇怪的值

标签 swift

尝试使用 Swift.Float 类型初始化字符串时,我得到了奇怪的值。

(lldb) po venue.rating().floatValue.dynamicType
Swift.Float

(lldb) po venue.rating().floatValue
9.2

(lldb) po String(format: "%.1f", [venue.rating().floatValue])
"30746195630460679147717820231031193600.0"

(lldb) po String(format: "%@", [venue.rating().floatValue])
"(\n    \"9.2\"\n)"

如何使用 Swift.Float 类型初始化 String

最佳答案

let someString = "\(someFloat)"
let someString2 = NSString(format: "%.3f", someFloat2) 

关于具有 Float 类型的 Swift String 初始值设定项生成奇怪的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33703271/

相关文章:

ios - 如何在 swift 查询返回时明智地从 HealthKits HKSample 中获取数据

ios - 以编程方式将 UILabel 添加到 CollectionViewCells

swift - AVPlayer/AVPlayerLayer 未出现在使用自动布局的 subview 中

ios - 一对一关系的值类型 Not Acceptable : property = "user"; desired type = User; given type = User;

ios - 避免快速重复代码

ios - 当我的游戏结束时,我需要 'send' 将分数保存到 TableView Controller 的变量

Xcode 6.1 编译错误

ios - 无法使用类型为 '*' 的参数列表调用 '(NSURL, *: NSString?)'

ios - 屏幕挂起并终止

swift - 在 Xcode 9 中运行自定义 shell 脚本 '[cp] embed pods frameworks'