将阿拉伯数字转化为人民币的大写
来源:互联网 发布:数组初始化为null 编辑:程序博客网 时间:2024/03/28 22:02
具体说明暂时没有,因为只需要将代码COPY进去,即可以直接使用!发出以下代码:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NumberToRMB {
private static String[] UNIT = { "元", "万", "亿" };
private static String[] UNIT2 = { "千", "百", "十" };
private static final String ZERO = "零";
private static String[] NUM = { "壹", "贰", "三", "肆", "伍", "陆", "柒", "捌",
"玖", "拾" };
/**
* 转换一串字符
*
* @param str
* @return
*/
public static String parseCharsToRMB(String str) {
if (!isNumber(str)) {
throw new IllegalArgumentException("参数不正确,必须为数字");
}
StringBuffer buffer = new StringBuffer();
int count = 0;
for (int i = str.length(); i > 0; i -= 4, count++) {
String char4 = null;
if (i - 4 < 0) {
System.out.println("start=" + 0 + ".end=" + i);
char4 = str.substring(0, i);
buffer.insert(0, parse4CharsToRMB(char4) + UNIT[count]);
break;
}
System.out.println("start=" + (i - 4) + ".end=" + i);
char4 = str.substring(i - 4, i);
buffer.insert(0, parse4CharsToRMB(char4) + UNIT[count]);
}
return buffer.toString();
}
/**
* 转换4位字符
*
* @param str
* @return
*/
public static String parse4CharsToRMB(String str) {
if (str.length() > 4 || !isNumber(str)) {
throw new IllegalArgumentException("参数不正确,str应该为不大于4位的字符");
}
StringBuffer result = new StringBuffer();
int startUNIT = getStartUNIT(str.length());
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') {
if (i == 0) {
result.append(ZERO);
} else {
appendZero(result, i);
}
startUNIT++;
} else {
int index = Integer.parseInt(String.valueOf(str.charAt(i))) - 1;
result.append(NUM[index]);
if (startUNIT < UNIT2.length) {
result.append(UNIT2[startUNIT]);
startUNIT++;
}
}
}
return result.toString();
}
/**
* 获取起始位置
*
* @param strLength
* @return
*/
public static int getStartUNIT(int strLength) {
switch (strLength) {
case 4:
return 0;
case 3:
return 1;
case 2:
return 2;
case 1:
return 3;
default:
return 0;
}
}
/**
* 0是特殊的。所以单独一个方法添加0
*
* @param buffer
* @param index
* @return
*/
private static StringBuffer appendZero(StringBuffer buffer, int index) {
System.out.println("buffer=" + buffer);
String before = buffer.substring(index, index + 1);
System.out.println("before=" + before);
if (before.equals(ZERO)) {
return buffer;
} else {
buffer.append(ZERO);
}
return buffer;
}
/**
* 判断是否为数字字符串
*
* @param str
* @return
*/
public static boolean isNumber(String str) {
if (str != null && str.length() > 0) {
Pattern pattern = Pattern.compile("[0-9]*");
Matcher m = pattern.matcher(str);
if (m.find()) {
return true;
}
return false;
}
return false;
}
public static void main(String[] args) {
System.out.println("转换1580654=" + parseCharsToRMB("1580654"));
}
}
- 将阿拉伯数字转化为人民币的大写
- 将阿拉伯数字转化成大写的人民币数字
- 将阿拉伯数字转化成大写的人民币数字
- 将阿拉伯数字转化成大写的人民币数字
- 将阿拉伯数字转化成大写的人民币数字
- 将人民币中的阿拉伯数字转换为大写
- 将人民币转化为大写
- 将小写数字转化为大写人民币
- 阿拉伯数字转化为中文大写
- 将输入数据转化为人民币大写输出
- C#将金额转化为人民币大写形式
- 将金额转化为人民币大写形式 javascript版
- PHP将数字金额转化为中文人民币大写
- 数字转化为人民币大写
- 金额的转化,将阿拉伯数字的金额装换成大写
- 能将人民币金额从阿拉伯数字转换为大写汉字表示
- 将人民币的数字转化成大写表示 [asp]
- 将人民币的数字转化成大写表示
- doc 的命令(全屏,进文件啊,等等...)
- junit源码学习-交织的TestCase和TestResult
- (二十七)光辉岁月
- MyEclipse使用技巧和快捷键有重复
- 创建一个看不见的文件夹
- 将阿拉伯数字转化为人民币的大写
- 单击鼠标自动锁定
- VS.net 和Reflector 图标解释
- U BOOT
- 下拉框树,HTML代码
- 介绍 IOC
- 生活、
- 路径问题path
- 继 CNTV.COM后雨林木风又获得一个重磅域名www.baidu.si