关于人民币大小写转换的程序

package trs.lyj.web;

import java.util.Scanner;

public class RMB {
 private static String[] shuju = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒",
   "捌", "玖" };

 public static void main(String[] args) {
  Scanner scan = new Scanner(System.in);
  String rmb = changetext(scan.nextLine());// 可以成功返回一批数据,但是还有些欠缺,主要是不符合我们的习惯

  System.out.println(rmb);
 }

 private static String changetext(String nextLine) {
  String str = "";
  if (nextLine.startsWith("0") || nextLine.length() > 12) {
   System.out.println("数据不合法");
  }
  nextLine = nextLine.replaceAll("\\s+", "");
  if (nextLine.contains(".")) {
   String[] line = nextLine.split("\\.");
   nextLine = line[0];
   int value = Integer.parseInt(line[1].substring(0, 2));
   str = shuju[value / 10] + "角" + shuju[value % 10] + "分";
  }
  // System.out.println(nextLine);
  String[] shuju2 = new String[(nextLine.length() % 4 == 0) ? (nextLine
    .length() / 4) : (nextLine.length() / 4) + 1];// 定义数段数组的大小
  int j = 0;
  if (nextLine.length() % 4 != 0) {
   shuju2[j] = nextLine.substring(0, nextLine.length() % 4);// 第一组数据
   int a = nextLine.length() % 4;
   for (int i = 1; i < shuju2.length; i++) {
    shuju2[i] = nextLine.substring(a, a + 4);
    a = a + 4;
   }
  } else {
   for (int i = 0; i < shuju2.length; i++) {
    shuju2[i] = nextLine.substring(j, j + 4);
    j = j + 4;
   }
  }

  System.out.println(shuju2.length);// 新数组长度
  // 到此为止正常
  String restr = "";
  if (shuju2.length == 3) {
   restr += parseShuju(shuju2[0]) + "亿" + parseShuju(shuju2[1]) + "万"
     + parseShuju(shuju2[2]) + "圆";
  }
  if (shuju2.length == 2) {
   restr += parseShuju(shuju2[0]) + "万" + parseShuju(shuju2[1]) + "圆";
  }
  if (shuju2.length == 1) {

   restr += parseShuju(shuju2[0]) + "圆";
  }
  return restr + str;
 }

 public static String parseShuju(String shu) {
  int line = Integer.parseInt(shu);
  int qian = line / 1000;
  int bai = (line % 1000) / 100;
  int shi = (line % 100) / 10;
  int ge = line % 10;
  String ssh = "";
  if (qian != 0) {
   ssh += shuju[qian] + "仟";
  }
  if (bai != 0) {
   ssh += shuju[bai] + "佰";
  }else if(qian!=0&&bai==0){
   ssh+="零";
  }
  if (shi != 0) {
   ssh += shuju[shi] + "拾";
  }
  if (ge != 0) {
   ssh += shuju[ge];
  }
  // ssh=shuju[qian]+"仟"+shuju[bai]+"佰"+shuju[shi]+"拾"+shuju[ge];
  return ssh;
 }

}

该程序对零的处理存在问题,基本就是没有处理,希望高人们给点意见。
//ChangeRMB.java /** * * programmed by HuangHeliang * 2009.04.15 10:20:51 * */ //package com.avtech.hhl; import java.io.*; public final class ChangeRMB { //每个数字对应的大写 private static final String[] num = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", }; //从低到高排列的单位 private static final String[] bit = { "圆", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿" }; //金额里面的角和分 private static final String[] jf={ "角","分" }; /** * 处理金额的整数部分,返回"...圆整" * @param integer * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer)throws Exception{ StringBuilder sbdr=new StringBuilder(""); int j=integer.length(); if(j>bit.length){ throw new Exception("\n只能处理亿万亿以内的数据(含亿万亿)!"); } char[] rmb=integer.toCharArray(); for (int i = 0; i 壹佰亿陆仟伍佰万肆仟伍佰捌拾叁圆零伍分 */ if(bit[bitLocate].equals("仟")){ String s=sbdr.toString(); if(!s.endsWith(bit[bitLocate+1]) && s.length()>0){ if (s.endsWith(num[0])) { sbdr.deleteCharAt(sbdr.length() - 1); } sbdr.append(bit[bitLocate+1]); } } sbdr.append(num[numLocate]); sbdr.append(bit[bitLocate]); }//end for /* * 去掉结尾"零"后,补全 */ if(sbdr.toString().endsWith(num[0])){ sbdr.deleteCharAt(sbdr.length()-1); sbdr.append("圆整"); }else{ sbdr.append("整"); } return sbdr.toString(); } /** * 处理带小数的金额,整数部分交由上一个方法处理,小数部分自己处理 * @param integer * @param decimal * @return String * @throws Exception */ public static String praseUpcaseRMB(String integer, String decimal)throws Exception{ String ret=ChangeRMB.praseUpcaseRMB(integer); ret=ret.split("整")[0]; //处理整数部分 StringBuilder sbdr=new StringBuilder(""); sbdr.append(ret); char[] rmbjf=decimal.toCharArray(); for(int i=0;i rmbDouble){ theInt-=1; } double theDecimal=rmbDouble-theInt; String integer=new Long((long)theInt).toString(); String decimal=""+Math.round(theDecimal*100); if(decimal.equals("0")){ result=ChangeRMB.praseUpcaseRMB(integer); }else{ result=ChangeRMB.praseUpcaseRMB(integer, decimal); } return result; } public static void main(String[] args) throws Exception{ System.out.print("输入小写人民币金额:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String in = br.readLine(); String result=ChangeRMB.doChangeRMB(in); System.out.println("\n"+"------------转换结果------------"); System.out.println(result); double d=54628569856.68; String ret=ChangeRMB.doChangeRMB(d); System.out.println("\n"+"------------转换结果------------"); System.out.println(ret); } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值