Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)相关的知识,希望对你有一定的参考价值。

是要能相互转换的,不要只给数字转成中文大写的,最大数字范围达到万亿。
(输入“100002.1”时最好显示“拾万零贰点壹”,而不是“拾万零仟零佰零拾贰点壹”(要是写不出也没关系哦!))。请各位高手指点指点我这个菜鸟,多谢!!写的好的话会加分的!

package test.format;import java.text.NumberFormat;import java.util.HashMap;public class SimpleMoneyFormat
public static final String EMPTY = "";public static final String ZERO = "零";public static final String ONE = "壹";public static final String TWO = "贰";public static final String THREE = "叁";public static final String FOUR = "肆"; public static final String FIVE = "伍";public static final String SIX = "陆";public static final String SEVEN = "柒"; public static final String EIGHT = "捌"; public static final String NINE = "玖"; public static final String TEN = "拾"; public static final String HUNDRED = "佰"; public static final String THOUSAND = "仟"; 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 SimpleMoneyFormat formatter = null;private HashMap chineseNumberMap = new HashMap(); private HashMap chineseMoneyPattern = new HashMap(); private NumberFormat numberFormat = NumberFormat.getInstance(); private SimpleMoneyFormat() numberFormat.setMaximumFractionDigits(4); numberFormat.setMinimumFractionDigits(2); numberFormat.setGroupingUsed(false); chineseNumberMap.put("0", ZERO); chineseNumberMap.put("1", ONE); chineseNumberMap.put("2", TWO); chineseNumberMap.put("3", THREE); chineseNumberMap.put("4", FOUR); chineseNumberMap.put("5", FIVE); chineseNumberMap.put("6", SIX); chineseNumberMap.put("7", SEVEN); chineseNumberMap.put("8", EIGHT); chineseNumberMap.put("9", NINE); chineseNumberMap.put(DOT, DOT); chineseMoneyPattern.put("1", TEN); chineseMoneyPattern.put("2", HUNDRED); chineseMoneyPattern.put("3", THOUSAND); chineseMoneyPattern.put("4", TEN_THOUSAND); chineseMoneyPattern.put("5", TEN); chineseMoneyPattern.put("6", HUNDRED); chineseMoneyPattern.put("7", THOUSAND); chineseMoneyPattern.put("8", HUNDRED_MILLION);
public static SimpleMoneyFormat getInstance() if (formatter == null) formatter = new SimpleMoneyFormat(); return formatter;
public String format(String moneyStr) checkPrecision(moneyStr); String result; result = convertToChineseNumber(moneyStr); result = addUnitsToChineseMoneyString(result); return result; public String format(double moneyDouble) return format(numberFormat.format(moneyDouble)); public String format(int moneyInt) return format(numberFormat.format(moneyInt));
public String format(long moneyLong) return format(numberFormat.format(moneyLong));
public String format(Number moneyNum) return format(numberFormat.format(moneyNum));
private String convertToChineseNumber(String moneyStr) String result; StringBuffer cMoneyStringBuffer = new StringBuffer(); for (int i = 0; i < moneyStr.length(); i++) cMoneyStringBuffer.append(chineseNumberMap.get(moneyStr.substring(i, i + 1)));
//拾佰仟万亿等都是汉字里面才有的单位,加上它们 int indexOfDot = cMoneyStringBuffer.indexOf(DOT); int moneyPatternCursor = 1; for (int i = indexOfDot - 1; i > 0; i--) cMoneyStringBuffer.insert(i, chineseMoneyPattern.get(EMPTY + moneyPatternCursor)); moneyPatternCursor = moneyPatternCursor == 8 ? 1 : moneyPatternCursor + 1; String fractionPart = cMoneyStringBuffer.substring(cMoneyStringBuffer.indexOf(".")); cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("."), cMoneyStringBuffer.length()); while (cMoneyStringBuffer.indexOf("零拾") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零拾"), cMoneyStringBuffer.indexOf("零拾") + 2, ZERO);
while (cMoneyStringBuffer.indexOf("零佰") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零佰"), cMoneyStringBuffer.indexOf("零佰") + 2, ZERO);
while (cMoneyStringBuffer.indexOf("零仟") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零仟"), cMoneyStringBuffer.indexOf("零仟") + 2, ZERO);
while (cMoneyStringBuffer.indexOf("零万") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零万"), cMoneyStringBuffer.indexOf("零万") + 2, TEN_THOUSAND);
while (cMoneyStringBuffer.indexOf("零亿") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零亿"), cMoneyStringBuffer.indexOf("零亿") + 2, HUNDRED_MILLION); while (cMoneyStringBuffer.indexOf("零零") != -1)
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零零"), cMoneyStringBuffer.indexOf("零零") + 2, ZERO);
if (cMoneyStringBuffer.lastIndexOf(ZERO) == cMoneyStringBuffer.length() - 1) cMoneyStringBuffer.delete(cMoneyStringBuffer.length() - 1, cMoneyStringBuffer.length()); cMoneyStringBuffer.append(fractionPart);
result = cMoneyStringBuffer.toString(); return result; private String addUnitsToChineseMoneyString(String moneyStr) String result; StringBuffer cMoneyStringBuffer = new StringBuffer(moneyStr); int indexOfDot = cMoneyStringBuffer.indexOf(DOT); cMoneyStringBuffer.replace(indexOfDot, indexOfDot + 1, YUAN); cMoneyStringBuffer.insert(cMoneyStringBuffer.length() - 1, JIAO); cMoneyStringBuffer.insert(cMoneyStringBuffer.length(), FEN); if (cMoneyStringBuffer.indexOf("零角零分") != -1)//没有零头,加整
cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零角零分"), cMoneyStringBuffer.length(), "整"); else if (cMoneyStringBuffer.indexOf("零分") != -1)//没有零分,加整 cMoneyStringBuffer.replace(cMoneyStringBuffer.indexOf("零分"), cMoneyStringBuffer.length(), "整");
else if(cMoneyStringBuffer.indexOf("零角")!=-1)
cMoneyStringBuffer.delete(cMoneyStringBuffer.indexOf("零角"),cMoneyStringBuffer.indexOf("零角")+2);
tmpBuffer.append("整"); result = cMoneyStringBuffer.toString(); return result; private void checkPrecision(String moneyStr)
int fractionDigits = moneyStr.length() - moneyStr.indexOf(DOT) - 1; if (fractionDigits > 2) throw new RuntimeException("金额" + moneyStr + "的小数位多于两位。"); //精度不能比分低 public static void main(String args[]) System.out.println(getInstance().format(new Double(10010001.01)));
参考技术A eclipse中用java实现中文和阿拉伯数字互转的方法如下:
import java.io.*;
import java.lang.IllegalArgumentException;
public class ConvertNum
/**
* 把金额阿拉伯数字转换为汉字表示,小数点后四舍五入保留两位
* 还有一种方法可以在转换的过程中不考虑连续0的情况,然后对最终的结果进行一次遍历合并连续的零
*/
public static String [] ChineseNum = new String[]"零","壹","贰","叁","肆","伍","陆","柒","捌","玖";
public static String NumToChinese(double num)
if(num > 99999999999999.99 || num < -99999999999999.99)
throw new IllegalArgumentException("参数值超出允许范围 (-99999999999999.99 ~ 99999999999999.99)!");
boolean negative = false;//正负标号
if(num<0)
negative = true;
num = num*(-1);

long temp = Math.round(num*100);
int numFen=(int)(temp%10);//分
temp=temp/10;
int numJiao = (int)(temp%10);//角
temp=temp/10;
//此时temp只包含整数部分
int [] parts =new int[20];//将金额整数部分分为在0-9999之间数的各个部分
int numParts = 0;//记录把原来金额整数部分分割为几个部分
for(int i=0;;i++)
if(temp == 0)
break;
int part = (int)(temp%10000);
parts[i] =part;
temp = temp/10000;
numParts++;

boolean beforeWanIsZero = true;//标志位,记录万的下一级是否为0
String chineseStr = "";
for(int i=0;i<numParts;i++)
String partChinese = partConvert(parts[i]);
if(i%2==0)
if("".equals(partChinese))
beforeWanIsZero = true;
else
beforeWanIsZero = false;

if(i!=0)
if(i%2==0)//亿的部分
chineseStr = "亿"+chineseStr;
else
if("".equals(partChinese)&&!beforeWanIsZero)// 如果“万”对应的 part 为 0,而“万”下面一级不为 0,则不加“万”,而加“零”
chineseStr = "零"+chineseStr;
else
if(parts[i-1]<1000&&parts[i-1]>0)//如果万的部分不为0,而万前面的部分小于1000大于0,则万后面应该跟零
chineseStr = "零"+chineseStr;
chineseStr = "万"+chineseStr;



chineseStr = partChinese + chineseStr;

if("".equals(chineseStr))//整数部分为0,则表示为零元
chineseStr = ChineseNum[0];
else if(negative)//整数部分部位0,但是为负数
chineseStr = "负" +chineseStr;
chineseStr = chineseStr + "元";
if(numFen==0&&numJiao==0)
chineseStr = chineseStr +"整";

else if(numFen==0)//0分
chineseStr = chineseStr +ChineseNum[numJiao] + "角";

else
if(numJiao==0)
chineseStr = chineseStr + "零" + ChineseNum[numFen] + "分";
else
chineseStr = chineseStr + ChineseNum[numJiao] + "角" + ChineseNum[numFen] + "分";

return chineseStr;

//转换拆分后的每个部分,0-9999之间
public static String partConvert(int partNum)
if(partNum<0||partNum>10000)
throw new IllegalArgumentException("参数必须是大于等于0或小于10000的整数");

String [] units = new String[]"","拾","佰","仟";
int temp = partNum;
String partResult = new Integer(partNum).toString();
int partResultLength = partResult.length();
boolean lastIsZero = true;//记录上一位是否为0
String chineseStr = "";
for(int i=0;i<partResultLength;i++)
if(temp == 0)//高位无数字
break;
int digit = temp%10;
if(digit == 0)
if(!lastIsZero)//如果前一个数字不是0则在当前汉字串前加零
chineseStr = "零"+chineseStr;
lastIsZero = true;

else
chineseStr = ChineseNum[digit] + units[i] +chineseStr;
lastIsZero = false;

temp =temp/10;

return chineseStr;

public static void main(String args [])
double num = 0;
System.out.println("请输入金额数字,-1退出");
try
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
catch(IOException e)
System.out.println(e.toString());

while(num!=-1)
System.out.println(num+NumToChinese(num));
try
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
num = Double.parseDouble(br.readLine());
catch(IOException e)
System.out.println(e.toString());


System.out.println("其他测试:");
System.out.println("100120: " + NumToChinese(100120));
System.out.println("25000000000005.999: " + NumToChinese(25000000000005.999));
System.out.println("45689263.626: " + NumToChinese(45689263.626));
System.out.println("0.69457: " + NumToChinese(0.69457));
System.out.println("253.0: " + NumToChinese(253.0));
System.out.println("0: " + NumToChinese(0));

参考技术B 最简单的...
public class T
public static void main(String[] args)
zhuanhuan(10001101);

private static void zhuanhuan(int s)
String[] c = "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" ;
String[] d = "元", "拾", "百", "仟", "万","拾万","百万","千万","亿" ;
String ss = s + "";//数字强行转换成字符串
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < ss.length(); i++)
String k = ss.substring(i, i + 1);
int f = Integer.parseInt(k);
if (f == 0)
int u = Integer.parseInt(ss.substring(i - 1, i));
if (u != 0)
buffer.append(c[0]);

else
buffer.append(c[f]);
buffer.append(d[ss.length() - i - 1]);


System.out.println(buffer.toString());

参考技术C 这个得自己写函数了。呵呵。我有个VB的,晚上可以发给你看看。

数字金额转换中文大写

<script type="text/javascript" language="javascript">
function Arabia_to_Chinese(Num)
{
 for(i=Num.length-1;i>=0;i--)
 {
  Num = Num.replace(",","")//替换tomoney()中的“,”
  Num = Num.replace(" ","")//替换tomoney()中的空格
 }
 Num = Num.replace("¥","")//替换掉可能出现的¥字符
 if(isNaN(Num))
 { //验证输入的字符是否为数字
  alert("请检查小写金额是否正确");
  return;
 }
 //字符处理完毕后开始转换,采用前后两部分分别转换
 part = String(Num).split(".");
 newchar = "";
 //小数点前进行转化
 for(i=part[0].length-1;i>=0;i--)
 {
  if(part[0].length > 10)
  {
   alert("位数过大,无法计算");
   return "";
  }//若数量超过拾亿单位,提示
  tmpnewchar = ""
  perchar = part[0].charAt(i);
  switch(perchar)
  {
   case "0": tmpnewchar="零" + tmpnewchar ;break;
   case "1": tmpnewchar="壹" + tmpnewchar ;break;
   case "2": tmpnewchar="贰" + tmpnewchar ;break;
   case "3": tmpnewchar="叁" + tmpnewchar ;break;
   case "4": tmpnewchar="肆" + tmpnewchar ;break;
   case "5": tmpnewchar="伍" + tmpnewchar ;break;
   case "6": tmpnewchar="陆" + tmpnewchar ;break;
   case "7": tmpnewchar="柒" + tmpnewchar ;break;
   case "8": tmpnewchar="捌" + tmpnewchar ;break;
   case "9": tmpnewchar="玖" + tmpnewchar ;break;
  }
  switch(part[0].length-i-1)
  {
   case 0: tmpnewchar = tmpnewchar +"元" ;break;
   case 1: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
   case 2: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
   case 3: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
   case 4: tmpnewchar= tmpnewchar +"万" ;break;
   case 5: if(perchar!=0)tmpnewchar= tmpnewchar +"拾" ;break;
   case 6: if(perchar!=0)tmpnewchar= tmpnewchar +"佰" ;break;
   case 7: if(perchar!=0)tmpnewchar= tmpnewchar +"仟" ;break;
   case 8: tmpnewchar= tmpnewchar +"亿" ;break;
   case 9: tmpnewchar= tmpnewchar +"拾" ;break;
  }
  newchar = tmpnewchar + newchar;
 }
 //小数点之后进行转化
 if(Num.indexOf(".")!=-1)
 {
  if(part[1].length > 2)
  {
   alert("小数点之后只能保留两位,系统将自动截断");
   part[1] = part[1].substr(0,2)
  }
  for(i=0;i<part[1].length;i++)
  {
   tmpnewchar = ""
   perchar = part[1].charAt(i)
   switch(perchar)
   {
    case "0": tmpnewchar="零" + tmpnewchar ;break;
    case "1": tmpnewchar="壹" + tmpnewchar ;break;
    case "2": tmpnewchar="贰" + tmpnewchar ;break;
    case "3": tmpnewchar="叁" + tmpnewchar ;break;
    case "4": tmpnewchar="肆" + tmpnewchar ;break;
    case "5": tmpnewchar="伍" + tmpnewchar ;break;
    case "6": tmpnewchar="陆" + tmpnewchar ;break;
    case "7": tmpnewchar="柒" + tmpnewchar ;break;
    case "8": tmpnewchar="捌" + tmpnewchar ;break;
    case "9": tmpnewchar="玖" + tmpnewchar ;break;
   }
   if(i==0)tmpnewchar =tmpnewchar + "角";
   if(i==1)tmpnewchar = tmpnewchar + "分";
   newchar = newchar + tmpnewchar;
  }
 }
 //替换所有无用汉字
 while(newchar.search("零零") != -1)
  newchar = newchar.replace("零零", "零");
 newchar = newchar.replace("零亿", "亿");
 newchar = newchar.replace("亿万", "亿");
 newchar = newchar.replace("零万", "万");
 newchar = newchar.replace("零元", "元");
 newchar = newchar.replace("零角", "");
 newchar = newchar.replace("零分", "");
 if (newchar.charAt(newchar.length-1) == "元" || newchar.charAt(newchar.length-1) == "角")
     newchar = newchar+"整"
 return newchar;
}
function printp(obj)
{
  if (obj.style.display=="none")
  {
   obj.style.display="";
  }
  else
  {
   obj.style.display="none";
  }
}
</script>

 

 

 

JS处理部分写好了,那么是如何调用呢?往下看,同样简单:假设要在下面的div中显示大写金额,可以这样调用:

1 <div><b>已付金额总额(大写):</b><script language="javascript">document.write(Arabia_to_Chinese("3,060.00"));</script></div>

也就是把需要转换大写金额的小写数字输入在Arabia_to_Chinese()函数中,然后用javaScript的document.write方法显示在网页上。

以上是关于Java 关于中文大写金额与阿拉伯数字 互相转换的问题(eclipse版)的主要内容,如果未能解决你的问题,请参考以下文章

java阿拉伯数字表示的金额转换成中文大写金额

关于数字金额转换大写金额,sql语句

java实现数字金额转换成汉字大写金额

Java中金额数字转换为大写数字

js怎么把数字金额转换成中文大写金额

小写数字自动转换成大写人民币金额两张表怎么在桌面找不同吗?