string-formatting - Double 的字符串格式

标签 string-formatting swift

<分区>

我似乎找不到一种方法来格式化 Double,在标签中显示时将精度限制在小数点后的几个位置。 也许我遗漏了一些重要的东西,但我在官方文档中找不到任何东西。

在此先感谢您的帮助!

最佳答案

使用 String 插值无法做到这一点。

您可以在 this Apple Dev Forum post 上找到关于此的很好的讨论。 .

您可以使用 NSNumberFormatterNSDateFormatter 来格式化输出。

或者,要使用类似 printf 的语法,您仍然可以使用 NSString:

var str = NSString(format: "Hello, world %.2f", 42.12312312313)

关于string-formatting - Double 的字符串格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24047374/

相关文章:

ios - SwiftUI 图像剪辑到边界

swift - Swift 2 中的函数错误处理

ios - 使用 Swift 2 和 WindowsAzureMobileService.framework 在 iOS 客户端上实现 Azure 移动应用程序身份验证(使用 Twitter)(客户端模式)

Java:字符串填充失败

delphi - 如何格式化 MinExtended80Denormal?

python将字符串更改为日期

python - Pandas 数据框格式化

wpf - 使用字符串文字在标签上绑定(bind)字符串格式

swift - 从 Swift 运行命令行工具

ios - 错误 : Redundant conformance of 'ViewController' to protocol with extension