数字转化成大写解析器,可以实现把16384位数的数字字符串转换成大写数字,扩展长度比较方便,只要增加单位和对应的位数就可以实现。
在此把源代码贴出来,方便大家进行讨论
import java.util.ArrayList;
//解析器
public class Interpret {
// 数字
private final String[] digits = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒",
"捌", "玖" };
// 数量单位
private final String[] units = { "", "拾", "佰", "仟", "万", "亿", "兆", "京",
"垓", "秭", "穰", "沟", "涧", "正", "载", "无" };
// 数量单位对应的位数
private final int[] pows = { 0, 1, 2, 3, 4, 8, 16, 32, 64, 128, 256, 512,
1024, 2048, 4096, 8192, 16384, Integer.MAX_VALUE };
private final StringBuffer buf = new StringBuffer();
// pre:前一位是否为零、cur:当前位是否为零、seg:当前单位是否全为零
private static boolean pre = false, cur = false, seg = false;
private static Interpret inter = null;
private Interpret() {
}
public static Interpret getInstance() {
if (inter == null)
inter = new Interpret();
return inter;
}
private ArrayList<AConver> convs = new ArrayList<AConver>();
private String numStr;
// 解析器计数器
private int count = 0;
public int getCount() {
return count;
}
public String conver(String numStr) throws Exception {
// 清空缓存
buf.delete(0, buf.length());
// 分析数字字符串
if (!numStr.matches("\\d*"))
throw new Exception("请输入整数!");
if ("".equals(numStr))
return "";
this.numStr = numStr.replaceAll("\\b0*", "");
if ("".equals(numStr))
return digits[0];
int convLen = 0; // 解析器长度
while (pows[convLen] < numStr.length())
convLen++;
// 添加单位解析器
for (int i = convs.size(); i < convLen; i++) {
if (i < 4) // 4对应万
convs.add(new Unit(i));
else
convs.add(new Complex(i));
}
// 进行解析
for (int i = convLen - 1; i >= 0; i--)
convs.get(i).conver();
return buf.toString();
}
// 内部类:单位解析器
abstract class AConver {
protected int unit;
protected int pow;
public abstract void conver();
}
// 内部类:简单单位解析器
class Unit extends AConver {
private Unit(int unit, int pow) {
this.unit = unit;
this.pow = pow;
count++;
}
public Unit(int unit) {
this(unit, pows[unit]);
}
public void conver() {
int end = numStr.length() - pow;
if (numStr.length() > pow) {
int num = Integer.parseInt(numStr.substring(end - 1, end));
pre = cur;
if (num != 0) {
//添加零
if (!pre && buf.length() > 0)
buf.append(digits[0]);
buf.append(digits[num]).append(units[unit]);
cur = true;
} else
cur = false;
}
}
}
// 内部类:复杂单位解析器
class Complex extends AConver {
private ArrayList<AConver> convs = new ArrayList<AConver>();
private Complex(int unit, int pow) {
this.unit = unit;
this.pow = pow;
count++;
// 添加解析器
if (unit < 4) // 4对应万
convs.add(new Unit(unit, pow));
else
for (int i = 0; i < unit; i++)
if (i < 4) // 4对应万
convs.add(new Unit(i, pows[i] + pow));
else
convs.add(new Complex(i, pows[i] + pow));
}
public Complex(int unit) {
this(unit, pows[unit]);
}
public void conver() {
for (int i = convs.size() - 1; i >= 0; i--) {
convs.get(i).conver();
seg |= cur;
}
// 添加单位
if (seg) {
buf.append(units[unit]);
pre = cur;
cur = true;
}
}
}
}
// 测试类
public class Client {
public static void main(String[] args) throws Exception {
StringBuffer strBuf = new StringBuffer("10000000000");
for (int i = 0; i < 10; i++) {
System.out.println(Interpret.getInstance().conver(
strBuf.delete(strBuf.length() - 1, strBuf.length())
.toString()));
System.out.println(Interpret.getInstance().getCount());
}
}
}
输出结果
壹拾亿万
19
万壹亿万
19
壹仟万
19
壹佰万
19
壹拾万
19
壹万
19
壹仟
19
壹佰
19
壹拾
19
壹
19