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("\\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","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));
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-08-01
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);
}
}
第2个回答  2012-08-01
你的程序我帮你改完了,你看看吧,结果没问题了。
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);
}
}

运行结果:
壹亿零壹万零壹佰元整
第3个回答  2012-08-01
/**只贴修改的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);
}
第4个回答  2012-08-01
修改最后一个打印语句
System.out.println(sb.toString().replaceAll("(零.)+", "零").replaceAll("零整", "元整"));

java如何将数字转为中文大写
Description: 将数字金额转换为大写中文金额 date: 2019年6月18日 下午10:52:27 \/ public class ConvertUpMoney { \/\/ 大写数字 private static final String[] NUMBERS = {"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"};\/\/ 整数部分的单位 private static final String[]...

java问题,编写程序将一个3位数转成对应的大写汉字,如“301”对应“三百...
b, c;String bi, si, ge;public static void main(String[] args) {DemoBigNumbers db = new DemoBigNumbers();System.out.println("请输入一个三位数");Scanner sc = new Scanner(System.in);int

...就是输入阿拉伯数字运行让他变成大写的汉字,比如:123变成壹贰叁之类...
\/\/等待输入!System.out.print("请输入一个阿拉伯数字: ");BufferedReader br=new BufferedReader(new InputStreamReader(System.in));\/\/得到输入!try { String input=br.readLine();for(int count=0;count

Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)_百度知 ...
import java.io.*;import java.lang.IllegalArgumentException;public class ConvertNum{ \/** * 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位 * 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零 *\/ public static String [] ChineseNum = new String[]...

如何用java写出将五个数转换成财会大写形式
如下是一个将阿拉伯数字转为汉字的类,需要者可收藏到自己的util类库中以备不时之需。public class ArabicToChineseUtils { static String[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿" };static char[] num...

如何用java编一个程序将输入的阿拉伯数字转换成大写的汉字??
HashMap<Integer, String> hashMap = new HashMap<Integer, String>();hashMap.put(0, "零");hashMap.put(1, "壹");hashMap.put(2, "贰");hashMap.put(3, "叁");hashMap.put(4, "肆");hashMap.put(5, "伍");hashMap.put(6, "陆");hashMap.put(7, "柒");hashMap.put...

JAVA如何把键盘输入的金额换转成中文显示。在线求解释~~~
\/** * 将10亿以内的阿拉伯数字转成汉字大写形式 * @author Hp * *\/public class TestNum { \/\/ 整数部分 private String integerPart; \/\/ 小数部分 private String floatPart; \/\/ 将数字转化为汉字的数组,因为各个实例都要使用所以设为静态 private static final char[] cnNumbers={...

JAVA编写 将阿拉伯数字转换成中文大写(要eclipse的完整版本)
import java.io.InputStreamReader;public class RMB{ public static void main(String[] args) throws IOException { int money=12345,i=0;System.out.println("初始化的字符串为:"+money);System.out.println("翔鹰提供---有事联系yanxiang20006@126.com");InputStreamReader reader=new Input...

Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)_百度知 ...
public static final String TEN_THOUSAND = "万"; public static final String HUNDRED_MILLION = "亿"; public static final String YUAN = "元"; public static final String JIAO = "角"; public static final String FEN = "分"; public static final String DOT = "."; private static...

使用java编写输入1011,输出一千零一十一的类
思路很简单 首先 需要有一个映射类映射中文和数字的关系 重要结构如下 mapping.properties zh_2=十zh_3=百zh_4=千 cn_0=零cn_1=一... 读取后 1011 后 首先你可以获取这个数字的位数 string input = "1011";int length = input.length;string LastStr = "";for(int i=0;i<length;i++)...

相似回答