swift - 将 double 0.93 转换为整数 93

标签 swift double

self.humidityLbl.text = "Humidity: \(self.currentWeather.humidity)%"

输出湿度:0.93%。如何在 Swift 中将 double 0.93 转换为整数 93?

最佳答案

使用 NumberFormatterpercent 样式来显示百分比 对用户的值(value)。这将为用户的使用正确的格式 语言环境。示例:

let humidity = 0.93

let formatter = NumberFormatter()
formatter.numberStyle = .percent

formatter.locale = Locale(identifier: "en") // English
print(formatter.string(for: humidity)!) // 93%

formatter.locale = Locale(identifier: "de") // German
print(formatter.string(for: humidity)!) // 93 %

formatter.locale = Locale(identifier: "tr") // Turkish
print(formatter.string(for: humidity)!) // %93

formatter.locale = Locale(identifier: "ar") // Arabic
print(formatter.string(for: humidity)!) // ٩٣ ٪؜

这里我将 formatter.locale 设置为不同的值 示范用途。如果您不分配值,则用户的 使用默认设置。

关于swift - 将 double 0.93 转换为整数 93,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48128861/

相关文章:

swift - 将语言设置为简体中文 (Zh-Hans) 在 IOS9 上不起作用

swift - BLE 在蓝牙设备附近执行功能

ios - ViewController 传感器在 swift 弹出后仍然触发

ios - 如何在 swift 中使用 alamofire 将任何文件上传到服务器

c - 使用宏扩展从 C 函数返回 double

swift - 在标签上显示可选的双数

c# - C#中的双重解析算法

swift - 运行框架内编写的 UITest

java - 将 double 转换为 int (java)

java - 在 Java 中,两个 double 相乘为零?