数字转金额中文大写

数字转金额中文大写

package org.ms;

import java.util.Stack;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Change {

public Stack<String> stack;

public Change() {
stack = new Stack<String>();
}

public void pushToStack(Stack<String> stack, char c) {
switch (c) {
case '0':
stack.push("零");
break;
case '1':
stack.push("壹");
break;
case '2':
stack.push("贰");
break;
case '3':
stack.push("叁");
break;
case '4':
stack.push("肆");
break;
case '5':
stack.push("伍");
break;
case '6':
stack.push("陆");
break;
case '7':
stack.push("柒");
break;
case '8':
stack.push("捌");
break;
case '9':
stack.push("玖");
break;
}
}

// 算位
public void pushToStack_(Stack<String> stack, String s) {
int digit1 = 5;
int digit2 = 9;
int digit3 = 1;

for (int i = s.length() - 1, j = 0, k = 0; i >= 0; i--) {
char c = s.charAt(i);
j++;
k++;
if (k == 2) {
stack.push("拾");
}
else if (k == 3) {
stack.push("佰");
}
else if (k == 4) {
stack.push("仟");
k = 0;
}

if (j == digit1) {
stack.push("万");
digit1 += 8;
}
if (j == digit2) {
stack.push("亿");
digit2 += 8;
}
if (j == digit3 && c == '0') {
digit3 += 4;
continue;
}
else
pushToStack(stack, c);
}

}

// 处理
public String popToString(Stack<String> stack) {
String s = "";
StringBuffer buffer = new StringBuffer();

while (stack.empty() == false) {
buffer.append((String) stack.pop());
}

s = buffer.toString();
s = s.replaceAll("零仟", "零");
s = s.replaceAll("零佰", "零");
s = s.replaceAll("零拾", "零");
s = s.replaceAll("[零]+", "零");
s = s.replaceAll("零万", "万");
s = s.replaceAll("零亿", "亿");
s = s.replace("零角", "");
s = s.replace("零分", "");
s = s.replace("零厘", "");

return s;
}

/******************* 阿 拉 伯 数 字 转 中 文 金 额 大 写 *******************/
public String ArabToChinese(String str) {

// 验证
boolean flag = false;
Pattern pat= Pattern.compile("^[^0][0-9]+(.[0-9]{1,3})$");
Matcher matcher = pat.matcher(str);
flag = matcher.find();

if(flag){

String s = "";

int index = str.indexOf('.');

if (index != -1) {
s = str.substring(index + 1);
str = str.substring(0, index);

this.pushToStack_(this.stack, str);
str = this.popToString(this.stack) + "圆";

if (s.length() == 1) {
s = s + "00";
}
if (s.length() == 2) {
s = s + "0";
}

for (int i = 2, j = 0; i >= 0; i--) {

char c = s.charAt(i);

switch (j) {
case 0:
this.stack.push("厘");
break;
case 1:
this.stack.push("分");
break;
case 2:
this.stack.push("角");
break;

default:
break;
}

this.pushToStack(this.stack, c);

j++;

}

s = this.popToString(this.stack);

str = str + s;

} else {
this.pushToStack_(this.stack, str);
str = this.popToString(this.stack) + "圆";
str = str.replace("零圆", "圆");
}
}
else{
System.out.println("金额写法不正确");
}

return str;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值