摘要:以中文的形式表示数字,在开具发票、收据的时候经常用到,尤其在金融领域。但数字的中文表示和其它语言有很大的不同,如中文以每4个数字(万)为一个小的分隔。 本文以测试驱动开发的方法,开发该功能。本程序只是用表示整数的中文形式,很容易扩展到long形式和表示人民币货币的表示形式。
/**
* @author Jeff
*
* Copyright (c) 复制或转载本文,请保留该注释。
*/
package chinese.utility.test;
import org.junit.Assert;
import org.junit.Test;
import chinese.utility.ChineseNumber;
public class ChinesNumberFormatter {
/**
* 单个数字 0--9
*/
@Test
public void testSingleLower() {
Assert.assertEquals("零", new ChineseNumber(0).lower());
Assert.assertEquals("五", new ChineseNumber(5).lower());
}
/**
* 10--99
*/
@Test
public void test2BitLower() {
Assert.assertEquals("六十八", new ChineseNumber(68).lower());
Assert.assertEquals("八十", new ChineseNumber(80).lower());
}
/**
* 100--999
*/
@Test
public void test3BitLower() {
Assert.assertEquals("五百零八", new ChineseNumber(508).lower());
Assert.assertEquals("八百", new ChineseNumber(800).lower());
}
/**
* 1000--9999
*/
@Test
public void test4BitLower() {
Assert.assertEquals("一千零八", new ChineseNumber(1008).lower());
Assert.assertEquals("一千零八十", new ChineseNumber(1080).lower());
}
/**
* > 9999
*/
@Test
public void test5BitLower() {
Assert.assertEquals("三十万零八千", new ChineseNumber(308000).lower());
Assert.assertEquals("三十万零八百", new ChineseNumber(300800).lower());
}
/**
* 缩写和大写一起测
*/
@Test
public void testAbbriation() {
Assert.assertEquals("拾捌", new ChineseNumber(18, true).upper());
}
}
/**
* @author Jeff
*
* Copyright (c) 复制或转载本文,请保留该注释。
*/
package chinese.utility;
public class ChineseNumber {
private static final int RADIX = 10;
private static final String[] LOWER_UNIT = { "", "十", "百", "千", "万", "十",
"百", "千", "亿", "十", "百", "千", "万", "十", "百", "千", "万" };
private String[] LOWER = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
private static final String[] UPPER_UNIT = { "", "拾", "佰", "仟", "万", "拾",
"佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "万" };
private String[] UPPER = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
private int number;
private boolean abbreviation;
public ChineseNumber(int number, boolean isShort) {
this.number = number;
this.abbreviation = isShort;
}
public ChineseNumber(int number) {
this(number, false);
}
public String lower() {
return format(LOWER, LOWER_UNIT);
}
public String upper() {
return format(UPPER, UPPER_UNIT);
}
private String format(final String[] chinese, final String[] unit) {
if (number == 0) {
return chinese[number];
}
if (abbreviation == true && canAbbreviate(number)) {
return formatShort(chinese, unit);
}
String result = "";
int leftNumber = number;
int rightNumber = 0;
int currentNumber = 0;
int bit = 0;
while (leftNumber > 0) {
rightNumber = currentNumber;
currentNumber = leftNumber % RADIX;
leftNumber = leftNumber / RADIX;
if (currentNumber > 0) {
result = chinese[currentNumber] + unit[bit] + result;
} else if (rightNumber > 0) {
result = chinese[currentNumber] + result;
}
if (bit % 4 == 0 && currentNumber == 0) {
result = unit[bit] + result;
}
bit++;
}
return result;
}
/**
* 缩写 18 为 十八 或 拾捌
*/
private String formatShort(String[] chinese, String[] unit) {
return unit[1] + chinese[number % RADIX];
}
/**
* 能否缩写
*/
private boolean canAbbreviate(int number2) {
if (number2 >= 9 && number2 <= 19) {
return true;
}
return false;
}
}