请实现以下函数把一个用阿拉伯字符串表示的自然数(串长大于0,小于13,串保证是合法的)转换成相应的中文字符串。例如1234567890,应输出“十二亿三千四百五十六万七千八百九十”

  1. package com.test;  
  2.   
  3. public class NumberProcess  {  
  4.   
  5.     private static final String[] UNITS = { """十""百""千""万""十""百""千""亿""十""百""千", };  
  6.     private static final String[] NUMS = { "零""一""二""三""四""五""六""七""八""九", };  
  7.   
  8.     /**  
  9.      *  阿拉伯数字转换成中文字符串  
  10.      *  @param value 要转换的数字  
  11.      *  @return 返回数字转后的中文字符串  
  12.      */  
  13.     public static String number2Chinese(int value) {  
  14.   
  15.         String result = ""//转译结果   
  16.   
  17.         for (int i = String.valueOf(value).length() - 1; i >= 0; i--) {  
  1.             int r = (int) (value / Math.pow(10, i));//value / Math.pow(10, i) 截位匹配单位   
  2.             result += NUMS[r % 10] + UNITS[i];  
  3.         }  
  4.   
  5.         result = result.replaceAll("零[十, 百, 千]""零");//匹配字符串中的 "零[十, 百, 千]" 替换为 "零"  
  6.         result = result.replaceAll("零+""零");//匹配字符串中的1或多个 "零" 替换为 "零"  
  7.         result = result.replaceAll("零([万, 亿])""$1");  
  8.         result = result.replaceAll("亿万""亿"); //亿万位拼接时发生的特殊情况  
  9.   
  10.         if (result.startsWith("一十")) { //判断是否以 "一十" 开头 如果是截取第一个字符  
  11.             result = result.substring(1);  
  12.         }  
  13.   
  14.         if (result.endsWith("零")) { //判断是否以 "零" 结尾 如果是截取除 "零" 外的字符  
  15.             result = result.substring(0, result.length() - 1);  
  16.         }  
  17.   
  18.         return result;  
  19.     }  
  20.   
  21.     public static void main(String[] args) {  
  22.         System.out.println(NumberProcess .number2Chinese(1234567890));  
  23.     }  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值