发布网友 发布时间:2022-04-24 14:27
共5个回答
热心网友 时间:2023-10-16 08:53
java 数字转大写汉字的完整例子,请参考:
package com.sitinspring;
import java.math.BigDecimal;
import java.util.Hashtable;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 将阿拉伯数字转换成汉语大写数字
* @author sitinspring
* @date 2008-03-25
*/
public class ChineseUpperCaser{
/**
* 用于存储整数部分
*/
private String integerPart;
/**
* 用于存储小数部分
*/
private String floatPart;
/**
* 用于存储0-9大写的哈希表
*/
private static final Map<String,String> ZerotoNineHt;
/**
* 用于存储十百千大写的哈希表
*/
private static final Map<Integer,String> thHuTenHt;
/**
* 用于存储万亿兆大写的哈希表
*/
private static final Map<Integer,String> wanYiZhaoHt;
static{
ZerotoNineHt=new Hashtable<String,String>();
ZerotoNineHt.put("0", "零");
ZerotoNineHt.put("1", "壹");
ZerotoNineHt.put("2", "贰");
ZerotoNineHt.put("3", "叁");
ZerotoNineHt.put("4", "肆");
ZerotoNineHt.put("5", "伍");
ZerotoNineHt.put("6", "陆");
ZerotoNineHt.put("7", "柒");
ZerotoNineHt.put("8", "捌");
ZerotoNineHt.put("9", "玖");
thHuTenHt=new Hashtable<Integer,String>();
thHuTenHt.put(0, "");
thHuTenHt.put(1, "拾");
thHuTenHt.put(2, "佰");
thHuTenHt.put(3, "仟");
wanYiZhaoHt=new Hashtable<Integer,String>();
wanYiZhaoHt.put(0, "");
wanYiZhaoHt.put(1, "万");
wanYiZhaoHt.put(2, "亿");
wanYiZhaoHt.put(3, "兆");
}
private static String getWanYiZhao(int level){
String retval="";
do{
retval+=wanYiZhaoHt.get(level % 4);
level-=3;
}while(level>3);
return retval;
}
/**
* 构造函数
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(float number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 构造函数
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(double number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 构造函数
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(int number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 构造函数
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(long number) throws NumberFormatException{
this(String.valueOf(number));
}
/**
* 构造函数
* @param number
* @throws NumberFormatException
*/
public ChineseUpperCaser(String number) throws NumberFormatException{
String formalNumber=formatNumber(number);
// 辟分以给整数部分和小数部分赋值
String[] arr=formalNumber.split("[.]");
if(arr.length==2){
// 有小数点
integerPart=arr[0];
floatPart=arr[1];
}
else{
// 无小数点
integerPart=arr[0];
}
}
public String toString(){
String retval="";
if(integerPart!=null){
retval+=parseIntegerPart();
}
if(floatPart!=null){
retval+=parseFloatPart();
}
else{
retval+="整";
}
return retval;
}
/**
* 得到整数部分的汉字大写表示
* @return
*/
private String parseIntegerPart(){
String retval="";
// 将整数部分逆序,因为需要反向读取
String reverseIntegerPart="";
for(int i=integerPart.length()-1;i>-1;i--){
reverseIntegerPart+=integerPart.charAt(i);
}
// 将整数部分按四位分段
Pattern p = Pattern.compile("\\d{4}",Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(reverseIntegerPart);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while (result) {
// 每找到四位放一个逗号
m.appendReplacement(sb, m.group(0) + ",");
result = m.find();
}
m.appendTail(sb);
// 按逗号劈分,得到四位分组数据的数组
String[] arr=sb.toString().split(",");
int j;
String str;
for(int i=arr.length-1;i>=0;i--){
String temp=arr[i];
// 阿拉伯数字转大写汉字加单位(千百十)
for(j=temp.length()-1;j>=0;j--){
str=String.valueOf(temp.charAt(j));
retval+=ZerotoNineHt.get(str)+thHuTenHt.get(j);
}
retval=retval.replaceAll("(零)($)", "$2");// 零在末尾则去掉
// 加单位(兆亿万)
retval+=getWanYiZhao(i);
}
// 零替换
retval=retval.replaceAll("(零[仟佰拾])", "零");
retval=retval.replaceAll("(零{2,})", "零");
retval=retval.replaceAll("(零)($)", "$2");// 零在末尾则去掉
return retval;
}
/**
* 得到小数部分的汉字大写表示
* @return
*/
private String parseFloatPart(){
String retval="点";
for(int i=0;i<floatPart.length();i++){
String temp=String.valueOf(floatPart.charAt(i));
retval+=ZerotoNineHt.get(temp);
}
return retval;
}
/**
* 对输入的字符串进行验证,如果不能转化为数字形式则抛出数字转化异常
* ,注意这是一个运行时异常(非检查型异常),程序不用显式捕获
* @param number
* @throws NumberFormatException
*/
private String formatNumber(String number) throws NumberFormatException{
return (new BigDecimal(number)).toString();
}
public static void main(String[] args){
String[] arr={"1.543524304302432","12.432423432","123.454235","1234","12345","123456","1234567",
"12345678","1234567","12345671","123456712","1234567123","12345671234","123456712345",
"1234567123456","12345671234567","123456712345678","12345671234567",
"12345671234567123456012345671234567123450780","0","00","000","0000","01","001","0001",
"00001","10","100","1000","10000","101","1001","10001","100001","1.23","21.234","243400031.233234",
"5400035.980","543.6545"};
//String[] arr={"0","00","000","0000","01","001","0001","00001","10","100","1000","10000","101","1001","10001","100001"};
//String[] arr={"1.23","21.234","243400031.233234","5400035.980","543.6545"};
for(String str:arr){
System.out.println("阿拉伯数字等于:"+str+" 大写汉字等于:"+new ChineseUpperCaser(str));
}
}
}
热心网友 时间:2023-10-16 08:53
import java.util.HashMap;
import java.util.Map;
/**
* @author uuwoxin
* 试了好久,感觉终于差不多了,不过估计还会有些遗漏,可以再调试下,增加些replaceAll步骤
*/
public class Test1 {
public static void main(String[] args) {
Test1 t = new Test1();
for (int i = 0; i < 100; i++) {
t.formatTo(i);
}
for (int i = 100; i < 200; i+=10) {
t.formatTo(i);
}
for (int i = 200; i < 1000; i+=100) {
t.formatTo(i);
}
for (int i = 1000; i < 10000; i+=1000) {
t.formatTo(i);
}
t.formatTo(990200023);
}
public void formatTo(int money) {
System.out.print(money + "\t=\t");
Map<Integer, String> moneyMap = new HashMap<Integer, String>();
Map<Integer, String> moneyMap2 = new HashMap<Integer, String>();
StringBuffer sb = new StringBuffer();
moneyMap.put(1, "壹");
moneyMap.put(2, "贰");
moneyMap.put(3, "叁");
moneyMap.put(4, "肆");
moneyMap.put(5, "伍");
moneyMap.put(6, "陆");
moneyMap.put(7, "柒");
moneyMap.put(8, "捌");
moneyMap.put(9, "玖");
moneyMap.put(0, "零");
moneyMap2.put(1, "元整");
moneyMap2.put(2, "拾");
moneyMap2.put(3, "佰");
moneyMap2.put(4, "仟");
moneyMap2.put(5, "万");
moneyMap2.put(6, "拾");
moneyMap2.put(7, "佰");
moneyMap2.put(8, "仟");
moneyMap2.put(9, "亿");
moneyMap2.put(10, "拾");
moneyMap2.put(11, "佰");
moneyMap2.put(12, "仟");
moneyMap2.put(13, "万");
String moneyString = Integer.toString(money);
int moneyLength = moneyString.length();
int num = moneyLength;
Character c;
Integer key;
for (int i = 0; i < moneyLength; i++) {
c = moneyString.charAt(i);
key = Character.getNumericValue(c);
if (i < moneyLength) { // 去掉了“-1”
sb.append(moneyMap.get(key)).append(moneyMap2.get(num));
num--;
}
// if (i == moneyLength - 1 && num == 1) {
// sb.append(moneyMap.get(key)).append("元整");
// }
}
String result = sb.toString().replaceAll("(零[拾|佰|仟|亿])*零万", "万");
result = result.replaceAll("(零[拾|佰|仟|万|亿])*零元整", "元整");
result = result.replaceAll("(零[拾|佰|仟|万|亿])+", "零");
System.out.println(result.equals("元整") ? "零元整" : result);
}
}
热心网友 时间:2023-10-16 08:54
你的程序我帮你改完了,你看看吧,结果没问题了。
package com.javens.test;
import java.util.HashMap;
import java.util.Map;
public class Test1 {
private static int num =0;
public static void main(String[] args) {
new Test1().formatTo(100010100);
}
public void formatTo(int money){
Map<Integer, String> moneyMap = new HashMap<Integer, String>();
Map<Integer, String> moneyMap2 = new HashMap<Integer, String>();
StringBuffer sb = new StringBuffer();
moneyMap.put(1, "壹");
moneyMap.put(2, "贰");
moneyMap.put(3, "叁");
moneyMap.put(4, "肆");
moneyMap.put(5, "伍");
moneyMap.put(6, "陆");
moneyMap.put(7, "柒");
moneyMap.put(8, "捌");
moneyMap.put(9, "玖");
moneyMap.put(0, "零");
//moneyMap2.put(1, null);
moneyMap2.put(2, "拾");
moneyMap2.put(3, "佰");
moneyMap2.put(4, "仟");
moneyMap2.put(5, "万");
moneyMap2.put(6, "十");
moneyMap2.put(7, "百");
moneyMap2.put(8, "千");
moneyMap2.put(9, "亿");
moneyMap2.put(10, "十");
moneyMap2.put(11, "百");
moneyMap2.put(12, "千");
moneyMap2.put(13, "万");
String moneyString = (money+"").trim().toString();
int moneyLength = moneyString.length();
num = moneyLength;
boolean flag=false;
StringBuffer sb1=new StringBuffer(moneyString);
String moneyString1=sb1.reverse().toString();
if(moneyLength>=9){
for(int j=4;j<8;j++){
Character c2 = moneyString1.charAt(j);
Integer key2 = c2.getNumericValue(c2);
if(key2!=0){
flag=true;
break;
}
}
}
for(int i=0;i<moneyLength;i++){
Character c = moneyString.charAt(i);
Integer key = c.getNumericValue(c);
if(i<moneyLength-1){
if(key==0){
if((num==5 && moneyLength<9 && moneyLength>=5) || (num==9 && moneyLength>=9)||(num==5 && moneyLength>=9 && flag==true)){
sb.append(moneyMap2.get(num));
}
if(i+1<moneyLength){
Character c1 =moneyString.charAt(i+1);
if(c1.getNumericValue(c1)!=0){
sb.append(moneyMap.get(key));
}
}
}else{
sb.append(moneyMap.get(key)).append(moneyMap2.get(num));
}
num--;
}
if(i==moneyLength-1&&num==1){
if(key==0){
sb.append("元整");
}else{
sb.append(moneyMap.get(key)).append("元整");
}
}
}
System.out.println(sb);
}
}
运行结果:
壹亿零壹万零壹佰元整
热心网友 时间:2023-10-16 08:55
/**只贴修改的formatTo(int money)方法这一部分了,希望有帮助*/
public void formatTo(int money) {
Map<Integer, String> moneyMap = new HashMap<Integer, String>();
Map<Integer, String> moneyMap2 = new HashMap<Integer, String>();
StringBuffer sb = new StringBuffer();
moneyMap.put(1, "壹");
moneyMap.put(2, "贰");
moneyMap.put(3, "叁");
moneyMap.put(4, "肆");
moneyMap.put(5, "伍");
moneyMap.put(6, "陆");
moneyMap.put(7, "柒");
moneyMap.put(8, "捌");
moneyMap.put(9, "玖");
moneyMap.put(0, "零"); //
moneyMap2.put(1, null);
moneyMap2.put(2, "拾");
moneyMap2.put(3, "佰");
moneyMap2.put(4, "仟");
moneyMap2.put(5, "万");
moneyMap2.put(6, "十");
moneyMap2.put(7, "百");
moneyMap2.put(8, "千");
moneyMap2.put(9, "亿");
moneyMap2.put(10, "十");
moneyMap2.put(11, "百");
moneyMap2.put(12, "千");
moneyMap2.put(13, "万");
String moneyString = (money + "").trim().toString();
int moneyLength = moneyString.length();
num = moneyLength;
//标记上一层是否为零
boolean isPrevNumberZero = false;
for (int i = 0; i < moneyLength; i++) {
Character c = moneyString.charAt(i);
Integer key = Character.getNumericValue(c);
// 如果是0 跳过,并把isPrevNumberZero设为true
if(key == 0 ){
isPrevNumberZero = true;
num--;
continue;
}
//如果上层是0,则sb字符串增加一个零
if(isPrevNumberZero){
sb.append(moneyMap.get(0));
isPrevNumberZero = false;
}
if (i < moneyLength - 1) {
sb.append(moneyMap.get(key)).append(moneyMap2.get(num));
num--;
}
if (i == moneyLength - 1 && num == 1) {
sb.append(moneyMap.get(key)).append("元整");
}
}
System.out.println(sb);
}
热心网友 时间:2023-10-16 08:55
修改最后一个打印语句
System.out.println(sb.toString().replaceAll("(零.)+", "零").replaceAll("零整", "元整"));