本文主要是介绍阿拉伯数字转中文大写的一个方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
采用了递归的方法
package src;
/**
*
* @author Mr.liu
* qq:187235687
*/
public class Numeric {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(getChinese(2147483648l,0));
}
/**
* 获得阿拉伯数字对应的中文
* 最大只支持到9千9百九十九亿9千9百九十九万9千9百九十九
* @param number 要转换的数字
* @param depth 递归深度,使用时候直接给0即可
* @return 数字的中文描述
*/
public static String getChinese(long number,int depth){
if(depth<0)
depth = 0;
String chinese = "";
String src = number+"";
if(src.charAt(src.length()-1)=='l' || src.charAt(src.length()-1)=='L' )
{
src = src.substring(0, src.length()-1);
}
System.out.println(src);
if(src.length()>4)
chinese = getChinese(Integer.parseInt(src.substring(0, src.length()-4)),depth+1)+
getChinese(Integer.parseInt(src.substring(src.length()-4, src.length())),depth-1);
else{
char prv = 0;
for(int i=0;i<src.length();i++){
switch(src.charAt(i)){
case '0':
if(prv != '0')
chinese = chinese+"零";
break;
case '1':
chinese = chinese+"一";
break;
case '2':
chinese = chinese+"二";
break;
case '3':
chinese = chinese+"三";
break;
case '4':
chinese = chinese+"四";
break;
case '5':
chinese = chinese+"五";
break;
case '6':
chinese = chinese+"六";
break;
case '7':
chinese = chinese+"七";
break;
case '8':
chinese = chinese+"八";
break;
case '9':
chinese = chinese+"九";
break;
}
prv = src.charAt(i);
switch(src.length()-1-i){
case 1://十
if(prv != '0')
chinese = chinese + "十";
break;
case 2://百
if(prv != '0')
chinese = chinese + "百";
break;
case 3://千
if(prv != '0')
chinese = chinese + "千";
break;
}
}
}
while(chinese.length()>0 && chinese.lastIndexOf("零")==chinese.length()-1)
chinese = chinese.substring(0,chinese.length()-1);
if(depth == 1)
chinese += "万";
if(depth == 2)
chinese += "亿";
return chinese;
}
}
/**
*
* @author Mr.liu
* qq:187235687
*/
public class Numeric {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(getChinese(2147483648l,0));
}
/**
* 获得阿拉伯数字对应的中文
* 最大只支持到9千9百九十九亿9千9百九十九万9千9百九十九
* @param number 要转换的数字
* @param depth 递归深度,使用时候直接给0即可
* @return 数字的中文描述
*/
public static String getChinese(long number,int depth){
if(depth<0)
depth = 0;
String chinese = "";
String src = number+"";
if(src.charAt(src.length()-1)=='l' || src.charAt(src.length()-1)=='L' )
{
src = src.substring(0, src.length()-1);
}
System.out.println(src);
if(src.length()>4)
chinese = getChinese(Integer.parseInt(src.substring(0, src.length()-4)),depth+1)+
getChinese(Integer.parseInt(src.substring(src.length()-4, src.length())),depth-1);
else{
char prv = 0;
for(int i=0;i<src.length();i++){
switch(src.charAt(i)){
case '0':
if(prv != '0')
chinese = chinese+"零";
break;
case '1':
chinese = chinese+"一";
break;
case '2':
chinese = chinese+"二";
break;
case '3':
chinese = chinese+"三";
break;
case '4':
chinese = chinese+"四";
break;
case '5':
chinese = chinese+"五";
break;
case '6':
chinese = chinese+"六";
break;
case '7':
chinese = chinese+"七";
break;
case '8':
chinese = chinese+"八";
break;
case '9':
chinese = chinese+"九";
break;
}
prv = src.charAt(i);
switch(src.length()-1-i){
case 1://十
if(prv != '0')
chinese = chinese + "十";
break;
case 2://百
if(prv != '0')
chinese = chinese + "百";
break;
case 3://千
if(prv != '0')
chinese = chinese + "千";
break;
}
}
}
while(chinese.length()>0 && chinese.lastIndexOf("零")==chinese.length()-1)
chinese = chinese.substring(0,chinese.length()-1);
if(depth == 1)
chinese += "万";
if(depth == 2)
chinese += "亿";
return chinese;
}
}
这篇关于阿拉伯数字转中文大写的一个方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!