欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现整数转化为中文大写金额的方法

程序员文章站 2022-05-03 07:50:01
在日常生活中,我们经常会将阿拉伯数字转化为中文大写的情况:"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰...

在日常生活中,我们经常会将阿拉伯数字转化为中文大写的情况:"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖", "拾", "佰", "仟", "万", "亿",因此自己就编写一个类,实现该功能的转化。
实现过程:
对于这个问题的实现,我们首先把数字按四位一个拆开(符合我们读数字的习惯),然后对这四位数,做出他的读法,具体代码如下:

private static string getnumberstr1000 (int num) { 
 if (num > 9999 || num < 0) { 
  return ""; 
 } 
 int q = num / 1000; 
 int b = (num / 100) % 10; 
 int s = (num / 10) % 10; 
 int g = num % 10; 
 stringbuffer sb = new stringbuffer(); 
 //千 
 if (q > 0) { 
  sb.append(number1[q]); 
  sb.append(number2[3]); 
 } 
 //百 
 if (b > 0) { 
  sb.append(number1[b]); 
  sb.append(number2[2]); 
 } else { 
  if (q != 0) { 
   sb.append(number2[0]); 
  } 
 } 
 //十 
 if (s > 0) { 
  sb.append(number1[s]); 
  sb.append(number2[1]); 
 } else { 
  if (b != 0) { 
   sb.append(number2[0]); 
  } 
 } 
 //个 
 if (g > 0) { 
  sb.append(number1[g]); 
 } 
 return sb.tostring(); 
} 

下面我们只需要在编写一个方法,实现每个四位数的读法以及对应的单位即可,具体代码如下:

public static string getnumberstr(int num) { 
 if (num < 0) { 
  return ""; 
 } 
 if (num == 0) { 
  return number1[0]; 
 } 
 int split = 10000; 
 int y = num / (split * split); 
 int w = (num / split) % split; 
 int g = num % split; 
 stringbuffer sb = new stringbuffer(); 
 //亿 
 if (y > 0) { 
  sb.append(getnumberstr1000(y)); 
  sb.append(number2[5]); 
 } 
 //万 
 if (w > 999) { 
  sb.append(getnumberstr1000(w)); 
  sb.append(number2[4]); 
 } else { 
  if (w > 0) { 
   if (y != 0) { 
    sb.append(number2[0]); 
   } 
   sb.append(getnumberstr1000(w)); 
   sb.append(number2[4]); 
  } 
 } 
 //万以下 
 if (g > 0) { 
  if (w != 0) { 
   if (g > 999) { 
    sb.append(getnumberstr1000(g)); 
   } else { 
    sb.append(number2[0]); 
    sb.append(getnumberstr1000(g)); 
   } 
    
  } else { 
   if (y != 0) { 
    sb.append(number2[0]); 
   } 
   sb.append(getnumberstr1000(g)); 
  } 
 } 
 return sb.tostring(); 
} 

完整源代码:

 /** 
 *@description:  
 */ 
package com.lulei.util; 
 
public class numberutil { 
 private static string[] number1 = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; 
 private static string[] number2 = {"零", "拾", "佰", "仟", "万", "亿"}; 
  
 /** 
  * @param num 
  * @return 
  * @author:lulei 
  * @description:将数字转化为大写 
  */ 
 public static string getnumberstr(int num) { 
  if (num < 0) { 
   return ""; 
  } 
  if (num == 0) { 
   return number1[0]; 
  } 
  int split = 10000; 
  int y = num / (split * split); 
  int w = (num / split) % split; 
  int g = num % split; 
  stringbuffer sb = new stringbuffer(); 
  //亿 
  if (y > 0) { 
   sb.append(getnumberstr1000(y)); 
   sb.append(number2[5]); 
  } 
  //万 
  if (w > 999) { 
   sb.append(getnumberstr1000(w)); 
   sb.append(number2[4]); 
  } else { 
   if (w > 0) { 
    if (y != 0) { 
     sb.append(number2[0]); 
    } 
    sb.append(getnumberstr1000(w)); 
    sb.append(number2[4]); 
   } 
  } 
  //万以下 
  if (g > 0) { 
   if (w != 0) { 
    if (g > 999) { 
     sb.append(getnumberstr1000(g)); 
    } else { 
     sb.append(number2[0]); 
     sb.append(getnumberstr1000(g)); 
    } 
     
   } else { 
    if (y != 0) { 
     sb.append(number2[0]); 
    } 
    sb.append(getnumberstr1000(g)); 
   } 
  } 
  return sb.tostring(); 
 } 
  
 /** 
  * @param num 
  * @return 
  * @description:对万以下的数字进行大小写转化 
  */ 
 private static string getnumberstr1000 (int num) { 
  if (num > 9999 || num < 0) { 
   return ""; 
  } 
  int q = num / 1000; 
  int b = (num / 100) % 10; 
  int s = (num / 10) % 10; 
  int g = num % 10; 
  stringbuffer sb = new stringbuffer(); 
  //千 
  if (q > 0) { 
   sb.append(number1[q]); 
   sb.append(number2[3]); 
  } 
  //百 
  if (b > 0) { 
   sb.append(number1[b]); 
   sb.append(number2[2]); 
  } else { 
   if (q != 0) { 
    sb.append(number2[0]); 
   } 
  } 
  //十 
  if (s > 0) { 
   sb.append(number1[s]); 
   sb.append(number2[1]); 
  } else { 
   if (b != 0) { 
    sb.append(number2[0]); 
   } 
  } 
  //个 
  if (g > 0) { 
   sb.append(number1[g]); 
  } 
  return sb.tostring(); 
 } 
 
 public static void main(string[] args) { 
  // todo auto-generated method stub 
  int i = 1; 
  system.out.println(i + "--" + numberutil.getnumberstr(i)); 
  i = 1001; 
  system.out.println(i + "--" + numberutil.getnumberstr(i)); 
  i = 100101; 
  system.out.println(i + "--" + numberutil.getnumberstr(i)); 
  i = 10100101; 
  system.out.println(i + "--" + numberutil.getnumberstr(i)); 
  i = 1234567890; 
  system.out.println(i + "--" + numberutil.getnumberstr(i)); 
 } 
 
}

 测试运行结果:

java实现整数转化为中文大写金额的方法

希望本文所述对大家学习java程序设计有所帮助。