问题描述
publicclassmy_simulate{publicstaticString[]chineseDigits=newString[]{"零","一","二","三","四","五","六","七","八","九"};publicstaticStringamountTochinese(doubleamount){if(amount>99999999999999.99||amount<-99999999999999.99)thrownewIllegalArgumentException("参数值超出允许范围(-99999999999999.99~99999999999999.99)!");booleannegative=false;if(amount<0){negative=true;amount=amount*(-1);}longtemp=Math.round(amount*100);intnumFen=(int)temp%10;temp=temp/10;intnumJiao=(int)temp%10;temp=temp/10;intparts[]=newint[20];intnumParts=0;for(inti=0;;i++){if(temp==0)break;intpart=(int)(temp%10000);parts[i]=part;numParts++;temp=temp/10000;}booleanbeforeWanIsZero=true;StringchineseStr="";for(inti=0;i<numParts;i++){StringpartChinese=partTranslate(parts[i]);if(i%2==0){if("".equals(partChinese))beforeWanIsZero=true;elsebeforeWanIsZero=false;}if(i!=0){if(i%2==0)chineseStr="亿"+chineseStr;else{if("".equals(partChinese)&&!beforeWanIsZero)chineseStr="零"+chineseStr;else{if(parts[i-1]<1000&&parts[i-1]>0)chineseStr="零"+chineseStr;chineseStr="万"+chineseStr;}}}chineseStr=partChinese+chineseStr;}if("".equals(chineseStr))chineseStr=chineseDigits[0];elseif(negative)chineseStr="负"+chineseStr;chineseStr=chineseStr+"元";if(numFen==0&&numJiao==0){chineseStr=chineseStr+"整";}elseif(numFen==0){chineseStr=chineseStr+numJiao+"角";}else{if(numJiao==0)chineseStr=chineseStr+"零"+numFen+"分";elsechineseStr=chineseStr+numJiao+"角"+numFen+"分";}returnchineseStr;}privatestaticStringpartTranslate(intamountPart){if(amountPart<0||amountPart>10000){thrownewIllegalArgumentException("参数必须是大于等于0,小于10000的整数!");}String[]units=newString[]{"","拾","佰","仟"};inttemp=amountPart;StringamountStr=newInteger(amountPart).toString();intamountStrLength=amountStr.length();booleanlastIsZero=true;StringchineseStr="";for(inti=0;i<amountStrLength;i++){if(temp==0)break;intdigit=temp%10;if(digit==0){if(!lastIsZero)chineseStr="零"+chineseStr;lastIsZero=true;}else{chineseStr=chineseDigits[digit]+units[i]+chineseStr;lastIsZero=false;}temp=temp/10;}returnchineseStr;}publicstaticvoidmain(String[]args){if(args.length==0){System.out.println("转换演示:");System.out.println("-------------------------");System.out.println("25000000000005.999:"+amountTochinese(25000000000005.68));System.out.println("445689263.626:"+amountTochinese(45689263.17));System.out.println("0.69457:"+amountTochinese(0.58657));System.out.println("253.0:"+amountTochinese(253.0));System.out.println("0:"+amountTochinese(0));System.out.println("-------------------------");}else{System.out.println("转换结果:");System.out.println(args[0]+":"+amountTochinese(Double.parseDouble(args[0])));}}}