java实现人民币数字转换成中文

public class test {
     /**
      * 人民币大写单位制
      */
     private static HashMap<Integer, String> dws;
     /**
      * 数字对应的中文
      */
     private static String[] jes;
     // 初始化执行
     static {
         dws = new HashMap<Integer, String>();
         dws.put(- 2 , "分" );
         dws.put(- 1 , "角" );
         dws.put( 0 , "元" );
         dws.put( 1 , "拾" );
         dws.put( 2 , "佰" );
         dws.put( 3 , "仟" );
         dws.put( 4 , "万" ); //
         dws.put( 5 , "拾" );
         dws.put( 6 , "佰" );
         dws.put( 7 , "仟" );
         dws.put( 8 , "亿" ); //
         dws.put( 9 , "拾" );
         dws.put( 10 , "佰" );
         dws.put( 11 , "仟" );
         dws.put( 12 , "万" );
         jes = new String[]{ "零" , "壹" , "贰" , "叁" , "肆" , "伍" , "陆" , "柒" , "捌" , "玖" };
     }
     /**
      * 数字转换人民币大写
      * @param number 数字 不支持科学数字
      * @return
      */
     public static String chinese(String number){
         StringBuffer su = new StringBuffer();
         // 整数部分
         number = delInvalidZero(number);
         String str = null ;
         // 小数部分
         String decimal = null ;
         if (number.contains( "." )){
             // 截取整数位
             str = number.split( "\\." )[ 0 ];
             decimal = number.split( "\\." )[ 1 ];
         } else {
             str = number;
         }
         // 判断是否存在整数位
         if (str.length()> 0 ){
             for ( int i = 0 ; i < str.length() ; i++) {
                 String context = str.substring(i, i+ 1 );
                 int pow = str.length()-i- 1 ;
                 Integer val = Integer.parseInt(context.toString());
                 // 获取中文单位
                 String sign = dws.get(pow);
                 // 获取中文数字
                 String name = jes[Integer.parseInt(context)];
                 if (val== 0 ){
                     if (pow% 4 != 0 ){ // 删除单位
                         sign = "" ;
                     }
                     if (i < str.length()- 1 ){
                         Integer val1 = Integer.parseInt(str.substring(i+ 1 , i+ 2 ));
                         if (val== 0 &&val==val1){
                             name = "" ;
                         }
                     } else if (i == str.length()- 1 ){
                         name = "" ;
                     }
                 }
                 su.append(name+sign);
             }
         }
         // 判断是否存在小数位
         if (decimal!= null ){
             str = decimal.substring( 0 , 1 );
             if (! "0" .equals(str)){
                 su.append(jes[Integer.parseInt(str)]+dws.get(- 1 ));
             }
             if (decimal.length()== 2 ){
                 str = decimal.substring( 1 , 2 );
                 if (! "0" .equals(str)){
                     su.append(jes[Integer.parseInt(str)]+dws.get(- 2 ));
                 }
             }
         } else {
             su.append( "整" );
         }
         return su.toString();
     }
     /**
      * 清理数字特殊字符
      * @param str
      * @return
      */
     private static String delInvalidZero(String str){
         if ( "0" .equals(str.substring( 0 , 1 ))){
             return delInvalidZero(str.substring( 1 , str.length()));
         } else if (str.contains( "," )){
             return delInvalidZero(str.replaceAll( "," , "" ));
         } else {
             return str;
         }
     }
     public static void main(String[] args) {
         System.out.println(test.chinese( "12.5" ));
     }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值