ios - 如何在swift 4中为每4位添加逗号或空格?
问题描述
我的情况有点特殊,我需要为每 4 位插入空格或逗号。
例子:
18686305
1868,6305 or 1868 6305
我该怎么做 swift 4?
解决方案
ANumberFormatter
旨在将数值转换为String
基于预定义格式的值。在您的情况下,以下内容将每四位插入分组分隔符:
import Foundation
let groupingSeparator = "," // determined based on user input, as per the question
let formatter = NumberFormatter()
formatter.positiveFormat = "####,####"
formatter.negativeFormat = "-####,####"
formatter.groupingSeparator = groupingSeparator
if let string = formatter.string(from: 18686305) {
print(string) // prints "1868,6305"
}
和变量遵循Unicode 技术标准 #positiveFormat
35 。negativeFormat
推荐阅读
- excel - 从sharepoint提取SSIS包中的excel表
- windows - PowerShell:排序对象降序错误
- r - 如何在ggplot中绘制带有NA的混合频率序列?
- python - Numpy 切片与列表切片
- swift - 按返回键后导航栏不隐藏
- c# - 将数据从一种形式传递到另一种形式(datagridview)
- spring-boot - 为 neo4j 中的所有集合附加默认前缀
- python - 无法使用 beautifulsoup 在网站上刮桌子
- java - 使用 BouncyCastle/Java 对 InputStream 进行 PGP 加密
- numpy - pyqgis:从列表中创建多层(自动)