数字转化成大写解析器,可以实现把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
分享到:
相关推荐
chinese_number_converter#中文数字转换器,#支持中文大写,#支持数字混合,#稍微不规范点能接受,特别不规范的中文数字会解析失败,如:万千个什么东西example:我们九千岁比起万岁爷也差不了多少啊我们9000岁比起...
该项目包括一个 Ruby 中的 ASCIImage 解析器,以及一个使用解析器将 ASCII 图像转换为 SVG 的脚本。 ASCII图像 格式已被为使用 NSStrings 在 iOS 和 OS X 上创建小图标的另一种简单方法。 原始代码可以在找到。 原则...
将数字转换为二进制后进行运算 只做整数运算,如果是小数的话,则去掉小数位再运算 2、位运算 1、按位 与 :& 语法 :a & b 特点 :将 a 和 b 先转换为二进制,按位比较,对应位置的数字都为1的话,那么该位的...
中文数字转换为阿拉伯数字.cmd 中文显示ping结果.bat 九九乘法表.cmd 九宫格图案.cmd 乱序重列某类文件.cmd 交换两个变量的值而不使用临时变量.bat 交换两个变量的值而不使用临时变量.cmd 人民币大小写互转程序.cmd ...
upper_case:将字符串转换成大写 trim:去掉字符串前后的空白字符 下面是集合的常用内建函数 size:获取序列中元素的个数 下面是数字值的常用内建函数 int:取得数字的整数部分,结果带符号 例如: ${test?html}...
字符串大写转化.asp 字符串小写转化.asp 字符串替换.asp 将数组元素连接为字符串.asp 构造制定字符串.asp 正向截取字符串.asp 正向查找字符串.asp ...
中文数字转换为阿拉伯数字.cmd 人民币大小写互转程序.cmd 人民币金额小写转大写.cmd 十进制转二进制.cmd 十进制转十六进制.cmd 字母大小写转换.cmd 把秒转换为天小时分秒的格式.cmd 阿拉伯与罗马数字互转程序.cmd ...
“输入和输出4个十六进制数字” 'PSP输出' 处理文件: “用大写字母(带有文件)制作文本” “智能文件删除(带遮罩)” “将文本写入文件并进行分析” “电子邮件解析器” “读取文件并覆盖文本并进行转换复制” ...
所有名称列均应转换为大写。 将有非英文名称。 除Unicode验证外,应按原样传递“地址”列。 请注意,“地址”字段中有逗号; 您的CSV解析将需要考虑到这一点。 逗号只会出现在带引号的字符串中。 FooDuration和...
89、原生JavaScript实现金额大写转换函数 90、原生JavaScript常用的正则表达式大收集 91、原生JavaScript实现窗体改变事件resize的操作(兼容所以的浏览器) 92、原生JavaScript用正则清除空格分左右 93、原生...
70 <br>0096 如何将数字转换为字符串 70 <br>0097 如何将字符串转换为数字 70 <br>0098 如何将数字转换为日期格式 71 <br>0099 如何将数字转换为货币格式 71 <br>0100 将字母全部转换大写 ...
实例151 如何将B转换成GB、MB和KB 196 实例152 0~N位数的任意组合 197 实例153 在数组中快速查找近似值 199 实例154 猴子选大王算法的实现 200 实例155 使用MD5算法对密码进行加密 201 实例156 凯撒密码算法的实现 ...
5.6 如果NULL定义成#define NULL((char *)0) ,不就可以向函数传入不加转换的NULL 了吗? 57 5.7 我的编译器提供的头文件中定义的NULL为0L。为什么? 57 5.8 NULL可以合法地用作函数指针吗? 57 5.9 如果NULL...
检视系统时钟的解析度,同时也是计时器解析度的最大值。 Contig 希望能够快速地将常用的档案进行磁碟重组吗?使用 Contig 最佳化个别档案,或是建立新的连续档案。 Ctrl2cap 这是一种核心模式驱动程式,...
检视系统时钟的解析度,同时也是计时器解析度的最大值。 Contig 希望能够快速地将常用的档案进行磁碟重组吗?使用 Contig 最佳化个别档案,或是建立新的连续档案。 Ctrl2cap 这是一种核心模式驱动程式,...
lowToUpper.java 小写字母转换成大写字母 lozengeStar.java 输出一个由*组成的菱形 multiplyTable.java 求乘法口诀表 narrowingConversion_1.java 缩减转换引发错误示例1 narrowingConversion_2.java 缩减转换...
5.6 如果NULL定义成#defineNULL((char*)0),不就可以向函数传入不加转换的NULL了吗? 5.7 我的编译器提供的头文件中定义的NULL为0L。为什么? 5.8 NULL可以合法地用作函数指针吗? 5.9 如果NULL和0作为空指针...