如何用JAVA实现数字转换为大写?
用JAVA的ArrayList类实现拉个不过有0连着的很不好处理
请高手解答
[解决办法]
不知道链接怎么会出错,还是把代码给你吧.
- Java code
public class NumToRMB{ public static void main(String[] args){ System.out.println(changeToBig(Double.parseDouble(args[0]))); } public static String changeToBig(double value){ char[] hunit={'拾','佰','仟'}; //段内位置表示 char[] vunit={'万','亿'}; //段名表示 char[] digit={'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'}; //数字表示 long midVal = (long)(value*100); //转化成整形 String valStr=String.valueOf(midVal); //转化成字符串 String head=valStr.substring(0,valStr.length()-2); //取整数部分 String rail=valStr.substring(valStr.length()-2); //取小数部分 String prefix=""; //整数部分转化的结果 String suffix=""; //小数部分转化的结果 //处理小数点后面的数 if(rail.equals("00")){ //如果小数部分为0 suffix="整"; } else{ suffix=digit[rail.charAt(0)-'0']+"角"+digit[rail.charAt(1)-'0']+"分"; //否则把角分转化出来 } //处理小数点前面的数 char[] chDig=head.toCharArray(); //把整数部分转化成字符数组 boolean preZero=false; //标志当前位的上一位是否为有效0位(如万位的0对千位无效) byte zeroSerNum = 0; //连续出现0的次数 for(int i=0;i<chDig.length;i++){ //循环处理每个数字 int idx=(chDig.length-i-1)%4; //取段内位置 int vidx=(chDig.length-i-1)/4; //取段位置 if(chDig[i]=='0'){ //如果当前字符是0 preZero=true; zeroSerNum++; //连续0次数递增 if(idx==0 && vidx >0 &&zeroSerNum < 4){ prefix += vunit[vidx-1]; preZero=false; //不管上一位是否为0,置为无效0位 } }else{ zeroSerNum = 0; //连续0次数清零 if(preZero){ //上一位为有效0位 prefix+=digit[0]; //只有在这地方用到'零' preZero=false; } prefix+=digit[chDig[i]-'0']; //转化该数字表示 if(idx > 0) prefix += hunit[idx-1]; if(idx==0 && vidx>0){ prefix+=vunit[vidx-1]; //段结束位置应该加上段名如万,亿 } } } if(prefix.length() > 0) prefix += '圆'; //如果整数部分存在,则有圆的字样 return prefix+suffix; //返回正确表示 }}
[解决办法]
- Java code
public static void changToBig(double num) { String[] big = {"零","壹","贰","叁","肆","伍","陆","柒","扒","玖"}; String[] bit = {"元","拾","百","千","万","十万","百万","千万","亿"}; String n = Double.toString(num); String last = n.substring(n.lastIndexOf(".")+1); String first = n.substring(0,n.lastIndexOf(".")); String print = ""; for(int i=0;i<first.length();i++) { print = print + big[Integer.parseInt(first.substring(i,i+1))]; print = print + bit[first.length()-i-1]; } for(int i=0;i<2;i++) { print = print + big[Integer.parseInt(last.substring(i,i+1))]; if(i==0) print = print + "角"; else print = print + "分"; } System.out.println(print); }
[解决办法]
.Net/C#/VB/T-SQL/Java/Script 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有)
http://www.cnblogs.com/Microshaoft/archive/2005/04/02/131008.html
public class Class1
{
public static String ConvertNumberToChinese(String x, String[] Nums, String[] Digits, String[] Units)
{
String S = ""; //返回值
int p = 0; //字符位置指针
int m = x.length() % 4; //取模
// 四位一组得到组数
int k = (m > 0 ? x.length() / 4 + 1 : x.length() / 4);
// 外层循环在所有组中循环
// 从左到右 高位到低位 四位一组 逐组处理
// 每组最后加上一个单位: "[万亿]","[亿]","[万]"
for (int i = k; i > 0; i--)
{
int L = 4;
if (i == k && m != 0)
{
L = m;
}
// 得到一组四位数 最高位组有可能不足四位
String s = x.substring(p, p + L);
int l = s.length();
// 内层循环在该组中的每一位数上循环 从左到右 高位到低位
for (int j = 0; j < l; j++)
{
//处理改组中的每一位数加上所在位: "仟","佰","拾",""(个)
int n = java.lang.Integer.parseInt(s.substring(j, j+1));
if (n == 0)
{
if ((j < l - 1)
&& (java.lang.Integer.parseInt(s.substring(j + 1, j + 1+ 1)) > 0) //后一位(右低)
&& !S.endsWith(Nums[n]))
{
S += Nums[n];
}
}
else
{
//处理 1013 一千零"十三", 1113 一千一百"一十三"
if (!(n == 1 && (S.endsWith(Nums[0]) | S.length() == 0) && j == l - 2))
{
S += Nums[n];
}
S += Digits[l - j - 1];
}
}
p += L;
// 每组最后加上一个单位: [万],[亿] 等
if (i < k) //不是最高位的一组
{
if (java.lang.Integer.parseInt(s) != 0)
{
//如果所有 4 位不全是 0 则加上单位 [万],[亿] 等
S += Units[i - 1];
}
}
else
{
//处理最高位的一组,最后必须加上单位
S += Units[i - 1];
}
}
return S;
}
// 测试程序
public static void main(String[] args) throws Exception
{
//数字 数组
String[] Nums = new String[] {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
//位 数组
String[] Digits = new String[] {"", "拾", "佰", "仟"};
//单位 数组
String[] Units = new String[] {"", "[万]", "[亿]", "[万亿]"};
System.out.println(ConvertNumberToChinese("111112100113", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("1100000000", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("1000000000", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("40000000013", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("40000000001", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("400000010000", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("40101031013", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("40101031113", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("101140101031013", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("100000001000013", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("100000001000113", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("100011003", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("10010103", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("10110013", Nums, Digits, Units));
System.out.println(ConvertNumberToChinese("130000", Nums, Digits, Units));
//System.in.read();
}
}
[解决办法]
- Java code
import java.math.BigInteger;import java.util.Scanner;public class NumToChinese { /** * 该算法是以每四个数字单位计算 * 分别分成三个单位数组 * 根据输入数字的长度,再以四取模来判断当前数字单位 * 数字转换成字符串再进行长度计算 * 由于和数组比较,所以长度必须减一 */ public static void ToChinese(BigInteger num) { String n[] = {"零","壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; String unit[] = { "", "拾" , "佰","仟"}; String unit1[] = { "亿", "万" }; StringBuilder chi = new StringBuilder(); for (int i = 0; i < num.toString().length(); i++) { chi.append(n[Integer.parseInt(String.valueOf(num.toString().charAt(i)))]); chi.append(unit[(num.toString().length()-i-1) % 4]); if ((num.toString().length()-i) % 4 == 1) { chi.append(unit1[(int) Math.floor((double) (num.toString().length()-i) / 4) % 2]); } } String ch = chi.toString(); ch = ch.replaceAll("零仟", "零"); ch = ch.replaceAll("零佰", "零"); ch = ch.replaceAll("零拾", "零"); while(ch.indexOf("零零")>0) {[ ch = ch.replaceAll("零零", "零"); } ch = ch.replaceAll("零万", "万"); ch = ch.replaceAll("零亿", "亿"); ch = ch.replaceAll("亿万", "亿"); System.out.println(ch.substring(0, ch.length()-1)); } public static void main(String[] args) { Scanner in = new Scanner(System.in); while(true) { BigInteger num = in.nextBigInteger(); if(num.equals(0)) { System.exit(0); } ToChinese(num); } }}
[解决办法]
- Java code
/** * 将金额数字转为大写,例如:123456.23 转换后为 拾贰万三仟四佰伍拾六圆贰角三分 */package lib;import java.util.StringTokenizer;public class NumberFormate { private String num; private String prefix; private String suffix; private static String STR = "0123456789."; public NumberFormate(String number) { num = number; // 字符合法性判断 if(NumberFormate.isAllRight(num)) { spit(num); } else { System.out.println("非法数据!"); } } public String Convert() { String temp = "圆"; int count = 0; // 整数部分转换 for(int i=prefix.length()-1;i>=0;i--) { count++; int j = Integer.parseInt(prefix.charAt(i) + ""); // 当前数字 if(j == 0) { if(!temp.startsWith("零") && count>1) { temp = "零" + temp; } // 万位、亿位上为零,需要添加单位 if((count-1)>0 && (count-1)%4==0) { if((count-1)%8 == 0) temp = "亿" + temp; else temp = "万" + temp; } } else { // 添加计量单位 if(count%2 == 0 && count%4 != 0) temp = "拾" + temp; else if((count+1)%4 == 0) temp = "佰" + temp ; else if(count%4 == 0) temp = "仟" + temp; else if((count-1)>0 && (count-1)%4==0 && (count-1)%8!=0) temp = "万" + temp; else if((count-1)>0 && (count-1)%8==0) temp = "亿" + temp; } // 数字转大写 temp = NumberFormate.numTochn(j) + temp; } // 小数部分转换 if(suffix.length()>3) { System.out.println("超过三位后的小数忽略!"); suffix.substring(0,3); } for(int i=0;i<suffix.length();i++) { int k = Integer.parseInt(suffix.charAt(i) + ""); if(k != 0) { temp += NumberFormate.numTochn(k); if(i == 0) temp += "分"; else if(i == 1) temp += "毫"; else if(i ==2) temp += "厘"; } } // 正负数 if(prefix.startsWith("-")) temp = "负" + temp; return temp; } /** * @function 整数部分、小数部分初始化 */ private void spit(String num) { StringTokenizer st = new StringTokenizer(num,"."); if(st.countTokens() == 1) prefix = st.nextToken(); else if(st.countTokens() == 2) { prefix = st.nextToken(); suffix = st.nextToken(); } } /** * @function 判断数据是否合法 */ public static boolean isAllRight(String num) { boolean flag = true; int i; // 正负数 int count = 0; // 计算小数点个数 // 不为空 if(num != null && !num.equals("")) { // 正负数 if(num.startsWith("-")) i = 1; else i = 0; for(;i<num.length()-1;i++) { if(STR.indexOf(num.charAt(i)) == -1) { flag = false; break; } if((num.charAt(i) + "").equals(".")) count++; } // 小数点后没数据 if(num.endsWith(".")) flag = false; // 不止一小 if(count > 1) flag = false; } return flag; } /** * @function 小写转大写 */ public static String numTochn(int i) { String temp = ""; switch(i) { case 0: temp = ""; break; case 1: temp = "壹"; break; case 2: temp = "贰"; break; case 3: temp = "叁"; break; case 4: temp = "肆"; break; case 5: temp = "伍"; break; case 6: temp = "陆"; break; case 7: temp = "柒"; break; case 8: temp = "捌"; break; case 9: temp = "玖"; break; default: break; } return temp; } public static void main(String[] args) { String s = "9876543212345678901206.011"; NumberFormate nf = new NumberFormate(s); System.out.println("整数:" + nf.prefix); System.out.println("小数:" + nf.suffix); System.out.println(nf.Convert()); }}
[解决办法]
cursor_wang,你程序的四舍五入有问题 ,例外单位超过亿就没法正确显示。改了改,优化了下代码:
- Java code
import java.io.*;public class Test { public static void main(String[] args) { String strNum=""; double dNum; System.out.println("输入:"); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); try{ strNum=br.readLine(); }catch(IOException e){} dNum=Double.parseDouble(strNum); System.out.println("四舍五入,精确到(分),转化成大写:"); System.out.println(changeToBig(dNum)); System.gc(); } public static String changeToBig(double value) { char[] hunit = { '拾', '佰', '仟' }; // 段内位置表示 char[] vunit = { '万', '亿' }; // 段名表示 char[] digit = { '零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖' }; // 数字表示 long midVal = (long) (value * 100+0.5); // 转化成整形,四舍五入 String valStr = String.valueOf(midVal); // 转化成字符串 String head = valStr.substring(0, valStr.length() - 2); // 取整数部分 String rail = valStr.substring(valStr.length() - 2); // 取小数部分 String prefix = ""; // 整数部分转化的结果 String suffix = ""; // 小数部分转化的结果 // 处理小数点后面的数 if (rail.equals("00")) { // 如果小数部分为0 suffix = "整"; } else { suffix = digit[rail.charAt(0) - '0'] + "角" + digit[rail.charAt(1) - '0'] + "分"; // 否则把角分转化出来 } // 处理小数点前面的数 char[] chDig = head.toCharArray(); // 把整数部分转化成字符数组 boolean preZero = false; // 标志当前位的上一位是否为有效0位(如万位的0对千位无效) byte zeroSerNum = 0; // 连续出现0的次数 for (int i = 0; i < chDig.length; i++) { // 循环处理每个数字 int idx = (chDig.length - i - 1) % 4; // 取段内位置 int vidx = (chDig.length - i - 1) / 4; // 取段位置 if (chDig[i] == '0') { // 如果当前字符是0 preZero = true; zeroSerNum++; // 连续0次数递增 if (idx == 0 && vidx > 0 && zeroSerNum < 4) { prefix += vunit[vidx - 1]; preZero = false; // 不管上一位是否为0,置为无效0位 } } else { zeroSerNum = 0; // 连续0次数清零 if (preZero) { // 上一位为有效0位 prefix += digit[0]; // 只有在这地方用到'零' preZero = false; } prefix += digit[chDig[i] - '0']; // 转化该数字表示 if (idx > 0) prefix += hunit[idx - 1]; if (idx == 0 && vidx > 0) { prefix+=vunit[(vidx+1)%2]; for(int j=0;j<(vidx+1)/2-1;j++){ prefix+=vunit[1]; } } } } if (prefix.length() > 0) prefix += '圆'; // 如果整数部分存在,则有圆的字样 return prefix + suffix; // 返回正确表示 }}
[解决办法]
import java.util.*;
class Test
{
public static void main(String[] args)
{
Test nt = new Test();
nt.getBig("1000000000000000000000000000000000000000000000000000000000000000000.011");
}
public void getBig(String number){
//String str = "**ab**e**f ";
//str = str.replaceAll("^[a]+", "A");
//System.out.println(str);
//String[] s = str.split( "\\*+");
// for(int i=0;i<s.length;i++){System.out.println(i+":"+s[i]);}
//String str = "010003000000000";
//System.out.println(str);
//str = str.replaceAll("(0{3})*$","x");
//System.out.println(str);
String[] danwei = {"拾","佰","仟","","亿","圆"};
//String str = "00123456789123456789.012";
String str = number;
double num = 0;
try{
num = Double.parseDouble(str);
}catch(Exception e){
System.out.println("数据不符合要求!");
}
String[] str_num = str.split("\\.");
String z_num = str_num[0].replaceAll("^[0]+","");//整数部分
String x_num = "";//小数部分
String r_xiaoshu = "";
if(str_num.length==2){
x_num = str_num[1];
r_xiaoshu = numTochn(Integer.parseInt(x_num.substring(0,1)))+"角";
if(x_num.length()>1){
r_xiaoshu += numTochn(Integer.parseInt(x_num.substring(1,2)))+"分";
}
if(r_xiaoshu.indexOf("零角零分")>-1){
danwei[5] = "圆整";
}
r_xiaoshu = r_xiaoshu.replaceAll("零角零分|零分", "").replaceAll("零角", "零");
}
else{
danwei[5] = "圆整";
}
//System.out.println("z_num=="+z_num);
//System.out.println("x_num=="+x_num);
String result = "";
String fuhao = "";
if(z_num.startsWith("-")){
fuhao = "负";
z_num = z_num.substring(1);
}
int tmp_length = z_num.length();
int tmp = 0;
String tmp_danwei = "";
for(int i=1;i<=tmp_length;i++){
if(i%4==1){
switch(tmp%2){
case 1:
tmp_danwei += danwei[3];
break;
case 0:
if (tmp == 0) {
tmp_danwei += danwei[5];
} else {
tmp_danwei += danwei[4];
}
break;
default:
break;
}
tmp++;
}
else if(i%4==2){
tmp_danwei += danwei[0];
}
else if(i%4==3){
tmp_danwei += danwei[1];
}
else if(i%4==0){
tmp_danwei += danwei[2];
}
result = numTochn(Integer.parseInt(z_num.substring(tmp_length-i,tmp_length-i+1)))+tmp_danwei+result;
tmp_danwei = "";
}
result = fuhao + result;
System.out.println(result);
result = result.replaceAll("零仟零佰零拾零", "").replaceAll("零仟零佰零拾", "零").replaceAll("零仟零佰", "零").replaceAll("零仟", "零")
.replaceAll("零佰零拾零", "").replaceAll("零佰零拾", "零").replaceAll("零佰", "零")
.replaceAll("零拾零", "").replaceAll("零拾", "零").replaceAll("零", "").replaceAll("零亿", "亿");
result += r_xiaoshu;
System.out.println(result);
}
/**
* @function 小写转大写
*/
public static String numTochn(int i) {
String temp = "";
switch(i) {
case 0:
temp = "零";
break;
case 1:
temp = "壹";
break;
case 2:
temp = "贰";
break;
case 3:
temp = "叁";
break;
case 4:
temp = "肆";
break;
case 5:
temp = "伍";
break;
case 6:
temp = "陆";
break;
case 7:
temp = "柒";
break;
case 8:
temp = "捌";
break;
case 9:
temp = "玖";
break;
default:
break;
}
return temp;
}
}