有时候工作中需要将数字转换成单词拼写出的形式,如果有现成的肯定不去自己写啦~ ICU Project就可以帮你完成。
因为项目用的是Java,所以你可以从这里下载Jar包来用,当然更通用的方式是把它当作依赖引用进来啦~
Maven
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>
<version>66.1</version>
</dependency>
Gradle
compile group: 'com.ibm.icu', name: 'icu4j', version: '66.1'
以下是个Groovy写的例子
RuleBasedNumberFormat ruleBasedNumberFormat = new RuleBasedNumberFormat(new Locale("EN", "US"), RuleBasedNumberFormat.SPELLOUT)
[
12345,
300,
1,
12,
98,
101
].each {
System.out.println(ruleBasedNumberFormat.format(it))
}
输出结果
twelve thousand three hundred forty-five
three hundred
one
twelve
ninety-eight
one hundred one
Enjoy it.