加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift如何将数值转换为可拼写的字符串

发布时间:2020-12-14 06:35:21 所属栏目:百科 来源:网络整理
导读:比如100,我们希望输出”one hundred”,或者更好的是输出中文的”一百” 这貌似要写不少行代码,不过事实上你想多了! ;) Cocoa为你提供了一件利器来完成这些事:NumberFormatter let f = NumberFormatter()f.numberStyle = .spellOutprint(f. string ( from : 1

比如100,我们希望输出”one hundred”,或者更好的是输出中文的”一百”

这貌似要写不少行代码,不过事实上你想多了! ;)

Cocoa为你提供了一件利器来完成这些事:NumberFormatter

let f = NumberFormatter()
f.numberStyle = .spellOut

print(f.string(from: 122100)!)

代码输出为:

one hundred twenty-two thousand one hundred

如果要输出指定国家的语言只需要改变locale即可,比如输出中文:

let f = NumberFormatter()
f.numberStyle = .spellOut
f.locale = Locale(identifier: "zh_CN")

print(f.string(from: 122100)!)

输出结果为:

十二万二千一百

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读