.Net/C#/VB/T-SQL/Java/Script 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有) http://www.cnblogs.com/Microshaoft/archive/2005/04/02/131008.htmlpublic 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[] {"", "[万]", "[亿]", "[万亿]"};
public static void main(String[] args) { String num = "1234567890123456.43"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "0.979"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "09999999999999999"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "10,001,000.09"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "01.107000"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "1.0778879"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "1,002,002,020,000,201.897675434"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); num = "201000000"; System.out.println(num + "-" + CurrencyUtil.toChinese(num)); } }
楼上的也是出错 转换大数不行 1000000000000000000000000000000000000000000000000000000000000000000.9 报异常: Exception in thread "main" java.lang.NumberFormatException: For input string: "1000000000000000000000000000000000000000000000000000000000000000000" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Long.parseLong(Long.java:415) at java.lang.Long.parseLong(Long.java:461) at CurrencyUtil.toChinese(CurrencyUtil.java:60) at CurrencyUtil.main(CurrencyUtil.java:220) 我还是觉得这个好一点,不是我写的,只是改了一下 http://blog.csdn.net/jinggangshi/archive/2007/10/29/1854264.aspx
楼上的也是出错 转换大数不行 1000000000000000000000000000000000000000000000000000000000000000000.9 报异常: Exception in thread "main " java.lang.NumberFormatException: For input string: "1000000000000000000000000000000000000000000000000000000000000000000 " at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48) at java.lang.Long.parseLong(Long.java:415) at java.lang.Long.parseLong(Long.java:461) at CurrencyUtil.toChinese(CurrencyUtil.java:60) at CurrencyUtil.main(CurrencyUtil.java:220) 现在人民币有那么长的吗,再说了,你去银行看一下,最多时24位数的数字,长的都认为是错误的或者乱输得数字,我们现在也只考虑到24位长的数字。
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; //返回正确表示
}
}
谁能给个好的算法啊
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);
}
http://www.cnblogs.com/Microshaoft/archive/2005/04/02/131008.htmlpublic 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();
}
}
大家可以参考一下这篇文章:
http://hi.baidu.com/zqeast/blog/item/64eacefcbb90bafefd037f3e.html
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);
}
}
}
/**
* 将金额数字转为大写,例如: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());
}
}测试结果:整数:4030609
小数:3245
超过三位后的小数忽略!
肆佰零叁万零陆佰零玖圆叁分贰毫肆厘
整数:9876543212345678901206
小数:011
玖拾捌万柒仟陆佰伍拾肆亿叁仟贰佰壹拾贰万叁仟肆佰伍拾陆亿柒仟捌佰玖拾万零壹仟贰佰零陆圆壹毫壹厘
也谢谢楼上提出错误和指出错误原因的朋友
还有4楼的朋友说少了8角,其实是少了1分,不是多了8角:)下面是一个改正:
原句:long midVal = (long)(value*100);//changeToBig(double value)函数第4行
改成:BigDecimal midVal = new BigDecimal(Math.round(value*100));错误原因楼上朋友rayleigh_w提出那样是精度原因比如传进1234567.9,输出new BigDecimal(value*100)是123456789.99999998509883880615234375
所以加上Math.round()进行取整即可得到正确答案
测试结果:
1234567890123456.43-壹仟贰佰叁拾肆万伍仟陆佰柒拾捌亿玖仟零壹拾贰万叁仟肆佰伍拾陆元肆角叁分
0.979-玖角柒分玖厘
09999999999999999-玖仟玖佰玖拾玖万玖仟玖佰玖拾玖亿玖仟玖佰玖拾玖万玖仟玖佰玖拾玖元
10,001,000.09-壹仟万零壹仟元玖分
01.107000-壹元壹角柒厘
1.0778879-壹元柒分柒厘
1,002,002,020,000,201.897675434-壹仟零贰万零贰拾亿零贰仟万零贰佰零壹元捌角玖分柒厘
201000000-贰亿零壹佰万元/**
* 金额转换处理类,a.小写转大写;(b.大写转小写)
* <ul>
* <li>处理能力:</li>
* <li>a.整数部分:9999999999999999(16位长,仟万亿级)</li>
* <li>b.小数部分:3位(可扩展,单位?),多于3位舍去(不四舍五入)</li>
* <li>可无限扩展,方法:IUNIT后依次递加合适的单位,并处理0出现在关键位置</li>
* </ul>
* @author JIM
* @version 2.1
*/
public class CurrencyUtil {
/** 大写数字 */
private static final String[] NUMBERS = { "零", "壹", "贰", "叁", "肆", "伍", "陆",
"柒", "捌", "玖" }; /** 整数部分的单位 */
private static final String[] IUNIT = { "元", "拾", "佰", "仟", "万", "拾", "佰",
"仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟" }; /** 小数部分的单位 */
private static final String[] DUNIT = { "角", "分", "厘" }; /** 整数部分长度限制,仟万亿级 */
private static int LIMIT = 16;
/**
* 得到中文金额。
* @return 中文金额
*/
public static String toChinese(String str) {
// 去掉","
String number = getFormatNum(str);
// 不是金额,直接返回
if (!isMoney(number)) {
return number;
}
// 整数部分数字
String integerStr;
// 小数部分数字
String decimalStr; // 初始化:格式数字;分离整数部分和小数部分;确定长度
if (number.indexOf(".") > 0) {
integerStr = number.substring(0, number.indexOf("."));
decimalStr = number.substring(number.indexOf(".") + 1);
} else if (number.indexOf(".") == 0) {
integerStr = "";
decimalStr = number.substring(1);
} else {
integerStr = number;
decimalStr = "";
} // integerStr去掉首0,不必去掉decimalStr的尾0(超出部分舍去)
if (!integerStr.equals("")) {
integerStr = Long.toString(Long.parseLong(integerStr));
if (integerStr.equals("0")) {
integerStr = "";
}
} // overflow超出处理能力,直接返回
if (integerStr.length() > LIMIT) {
System.out.println(number + ":超出处理能力");
return number;
}
// 整数部分数字
int[] integers = toArray(integerStr);
// 设置万单位
boolean isMust5 = isMust5(integerStr);
// 小数部分数字
int[] decimals = toArray(decimalStr); // 返回大写金额
return getChineseInteger(integers, isMust5) + getChineseDecimal(decimals);
} /**
* 整数部分和小数部分转换为数组,从高位至低位
*/
private static int[] toArray(String number) {
int[] array = new int[number.length()];
for (int i = 0; i < number.length(); i++) {
array[i] = Integer.parseInt(number.substring(i, i + 1));
}
return array;
} /**
* 得到中文金额的整数部分。
* @return 整数部分
*/
private static String getChineseInteger(int[] integers, boolean isMust5) {
StringBuffer chineseInteger = new StringBuffer("");
String key = "";
int length = integers.length;
// int lastNum = 1;
// int lengthOfZero = 0;
for (int ii = 0; ii < length; ii++) {
// 1234(万)5678(亿)9012(万)3456(元)
// 0出现在关键位置:元、万、亿、万亿、兆、万兆、亿兆、万亿兆
// 处理顺序:不限,只跟具体位置有关,不必从高位到低位
// 特殊情况:10(拾元、壹拾元、壹拾万元、拾万元)
if (integers[ii] == 0) {
// 万(亿)(必填)
if ((length - ii) == 13)
key = IUNIT[4];
// 亿(必填)
else if ((length - ii) == 9)
key = IUNIT[8];
// 万(不必填)
else if ((length - ii) == 5 && isMust5)
key = IUNIT[4];
// 元(必填)
else if ((length - ii) == 1)
key = IUNIT[0]; // 0遇非0时补零,不包含最后一位
if ((length - ii) > 1 && integers[ii + 1] != 0)
key += NUMBERS[0];
// 其余情况
else
key += "";
// if(ii==5) break;
}
chineseInteger.append(integers[ii] == 0 ? key
: (NUMBERS[integers[ii]] + IUNIT[length - ii - 1]));
key = "";
}
return chineseInteger.toString();
} /**
* 得到中文金额的小数部分。
* @return 小数部分
*/
private static String getChineseDecimal(int[] decimals) {
StringBuffer chineseDecimal = new StringBuffer("");
for (int ii = 0; ii < decimals.length; ii++) {
// 舍去3位小数之后的
if (ii == 3)
break;
chineseDecimal.append(decimals[ii] == 0 ? ""
: (NUMBERS[decimals[ii]] + DUNIT[ii]));
}
return chineseDecimal.toString();
} /**
* 格式化数字:去掉","。
* @param num 数字
* @return 格式化后的数字
*/
private static String getFormatNum(String number) {
return StringUtil.replace(number, ",", "");
} /**
* 判断第5位数字的单位"万"是否应加。
* @return true是false否
*/
private static boolean isMust5(String integerStr) {
int length = integerStr.length();
if (length > 4) {
String subInteger = "";
if (length > 8) {
// 取得从低位数,第5到第8位的字串
subInteger = integerStr.substring(length - 8, length - 4);
} else {
subInteger = integerStr.substring(0, length - 4);
}
return Integer.parseInt(subInteger) > 0;
} else {
return false;
}
} /**
* 判断金额字符串是否合法 不合法:a.包含多于一个".";b.包含"."、","和数字以外的字符;c.¥/$?。
* @return true是false否
*/
private static boolean isMoney(String number) {
try {
Double.parseDouble(number);
} catch (Exception ex) {
System.out.println(number + "不是合法金额");
return false;
}
return true;
}
public static void main(String[] args) {
String num = "1234567890123456.43";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "0.979";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "09999999999999999";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "10,001,000.09";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "01.107000";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "1.0778879";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "1,002,002,020,000,201.897675434";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
num = "201000000";
System.out.println(num + "-" + CurrencyUtil.toChinese(num));
}
}
转换大数不行
1000000000000000000000000000000000000000000000000000000000000000000.9
报异常:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1000000000000000000000000000000000000000000000000000000000000000000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:415)
at java.lang.Long.parseLong(Long.java:461)
at CurrencyUtil.toChinese(CurrencyUtil.java:60)
at CurrencyUtil.main(CurrencyUtil.java:220)
我还是觉得这个好一点,不是我写的,只是改了一下
http://blog.csdn.net/jinggangshi/archive/2007/10/29/1854264.aspx
这个测试能通过吗?
转换大数不行
1000000000000000000000000000000000000000000000000000000000000000000.9
报异常:
Exception in thread "main " java.lang.NumberFormatException: For input string: "1000000000000000000000000000000000000000000000000000000000000000000 "
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Long.parseLong(Long.java:415)
at java.lang.Long.parseLong(Long.java:461)
at CurrencyUtil.toChinese(CurrencyUtil.java:60)
at CurrencyUtil.main(CurrencyUtil.java:220) 现在人民币有那么长的吗,再说了,你去银行看一下,最多时24位数的数字,长的都认为是错误的或者乱输得数字,我们现在也只考虑到24位长的数字。
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; // 返回正确表示
}
}
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;
}}
public class Test { public static void main(String[] args) throws IOException {
String strNum="";
double dNum;
System.out.println("输入:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
strNum=br.readLine();
dNum=Double.parseDouble(strNum);
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; // 返回正确表示
}
}
我运行过了,可以!用的是Netreans6.5.
import java.util.HashMap;
import java.util.Map;
public class SimpleMoneyFormat {
private static String[] danwei={"元","十","百","千","万","十","百","千","亿"};
private static String[] chinastr={"元","十","百","千","万","十","百","千","亿"};
private static Map<Integer,String> map=new HashMap<Integer,String>();
private static int flag=0;
private static int iszero=0;
public static void main(String[] args) {
String englishstr="10000700";
String formatstr="";
map.put(1, "壹");
map.put(2, "贰");
map.put(3, "叁");
map.put(4, "肆");
map.put(5, "伍");
map.put(6, "陆");
map.put(7, "漆");
map.put(8, "捌");
map.put(9, "玖");
map.put(0, "零");
for (int i = englishstr.length(); i >0 ; i--) {
if(englishstr.charAt(englishstr.length()-i)=='0'){
flag=flag+1;
if(flag==4&&formatstr.indexOf("万")==-1) formatstr=formatstr+"万";
iszero=1;}
else
flag=0;
if(flag==0){
if(iszero==1)formatstr=formatstr+"零";
formatstr = formatstr+map.get(Integer.parseInt(englishstr.charAt(englishstr.length()-i)+""))+danwei[i-1];
iszero=0;}
}
System.out.println(formatstr);
}
}