java 数字转大写汉字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 数字转大写汉字相关的知识,希望对你有一定的参考价值。

package com.javens.test;import java.util.HashMap;import java.util.Map;/** *  * @author javens * 偶尔网上看到这个题目,现在做不下去了!求高手继续做! * 程序已经基本完毕,遇到零后,最后的字符串未做处理! * 最后在写一个方法,把字符串传过去,做去零的处理,应该就没问题了! * 先运行,不要输入零的数字试试看,比如123112,然后在输入整数带零,比如10000,10020等等 *你就明白我的意思了,Now ,GO ON! */public class Test1  private static int num =0; public static void main(String[] args)   new Test1().formatTo(456456);   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;  for(int i=0;i<moneyLength;i++)   Character c = moneyString.charAt(i);   Integer key = c.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("元整");       System.out.println(sb);  

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("\\\\d4",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","123456789","1234567891","12345678912","123456789123","1234567891234","12345678912345",
"123456789123456","1234567891234567","12345678912345678","123456789123456789",
"123456789123456789123456089123456789123456789123450780","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));


参考技术A 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);

参考技术B 你的程序我帮你改完了,你看看吧,结果没问题了。
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);



运行结果:
壹亿零壹万零壹佰元整
参考技术C /**只贴修改的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);
参考技术D 修改最后一个打印语句
System.out.println(sb.toString().replaceAll("(零.)+", "零").replaceAll("零整", "元整"));

数字金额转大写汉字

function changeMoneyToChinese(money) {
var cnNums = new Array("零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"); //汉字的数字
var cnIntRadice = new Array("", "拾", "佰", "仟"); //基本单位
var cnIntUnits = new Array("", "万", "亿", "兆"); //对应整数部分扩展单位
var cnDecUnits = new Array("角", "分", "毫", "厘"); //对应小数部分单位
//var cnInteger = "整"; //整数金额时后面跟的字符
var cnIntLast = "元"; //整型完以后的单位
var maxNum = 999999999999999.9999; //最大处理的数字

var IntegerNum; //金额整数部分
var DecimalNum; //金额小数部分
var ChineseStr = ""; //输出的中文金额字符串
var parts; //分离金额后用的数组,预定义
if(money == "") {
return "";
}
money = parseFloat(money);
if(money >= maxNum) {
$.alert(‘超出最大处理数字‘);
return "";
}
if(money == 0) {
//ChineseStr = cnNums[0]+cnIntLast+cnInteger;
ChineseStr = cnNums[0] + cnIntLast
//document.getElementById("show").value=ChineseStr;
return ChineseStr;
}
money = money.toString(); //转换为字符串
if(money.indexOf(".") == -1) {
IntegerNum = money;
DecimalNum = ‘‘;
} else {
parts = money.split(".");
IntegerNum = parts[0];
DecimalNum = parts[1].substr(0, 4);
}
if(parseInt(IntegerNum, 10) > 0) { //获取整型部分转换
zeroCount = 0;
IntLen = IntegerNum.length;
for(i = 0; i < IntLen; i++) {
n = IntegerNum.substr(i, 1);
p = IntLen - i - 1;
q = p / 4;
m = p % 4;
if(n == "0") {
zeroCount++;
} else {
if(zeroCount > 0) {
ChineseStr += cnNums[0];
}
zeroCount = 0; //归零
ChineseStr += cnNums[parseInt(n)] + cnIntRadice[m];
}
if(m == 0 && zeroCount < 4) {
ChineseStr += cnIntUnits[q];
}
}
ChineseStr += cnIntLast;
//整型部分处理完毕
}
if(DecimalNum != ‘‘) { //小数部分
decLen = DecimalNum.length;
for(i = 0; i < decLen; i++) {
n = DecimalNum.substr(i, 1);
if(n != ‘0‘) {
ChineseStr += cnNums[Number(n)] + cnDecUnits[i];
}
}
}
if(ChineseStr == ‘‘) {
//ChineseStr += cnNums[0]+cnIntLast+cnInteger;
ChineseStr += cnNums[0] + cnIntLast;
}
/* else if( DecimalNum == ‘‘ ){
ChineseStr += cnInteger;
ChineseStr += cnInteger;
} */
return ChineseStr;
}

var bigMoney = changeMoneyToChinese(3000.10);
console.log(bigMoney)

以上是关于java 数字转大写汉字的主要内容,如果未能解决你的问题,请参考以下文章

转一个财务方面常用到的数字金额转成汉字大写金额 php类

阿拉伯数字 转 汉字大写

将数字金额转成汉字大写的

js金额转大写数字

金额数字转中文大写

Java将数字转为汉字(中国钱币-数字转汉字)