【发布时间】:2021-02-23 16:31:23
【问题描述】:
如何在字符之间添加空格? (百、千、万) 例如
550
5 500
55 500
555 500
5 555 000 等
我做了这个
@IBAction func textEditingChanged(_ sender: UITextField) {
if sender.text!.count > 0 && sender.text!.count % 4 == 0 && sender.text!.last! != " " {
sender.text!.insert(" ", at:sender.text!.index(sender.text!.startIndex, offsetBy: sender.text!.count-3) )
}
}
但它不能正常工作
5000
5 00000
然后用新空格删除
5 000...0
【问题讨论】:
-
添加空格的逻辑是什么?在每第三位数字之后?比如几千、几十万等等
-
@SandeepBhandari 是的,完全正确
-
@LeoDabus 没有扩展,拜托
-
您应该知道,数字之间的间距并不普遍是从右边开始的,以 3 为一组,或使用空格。这些东西中的每一个都可以在其他语言中有所不同。您应该使用 NumberFormatter 来正确执行此操作。
标签: swift uitextfield letter-spacing