您当前的位置: 首页 >  swift

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

发布时间:2016-12-01 15:53:26 ,浏览量:0

比如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)!)

输出结果为:

十二万二千一百

关注
打赏
1688896170
查看更多评论

暂无认证

  • 0浏览

    0关注

    107766博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0945s