import java.util.*;
public class? NumToChinese{
? public String? numToChinese(String money){
??? String s1="零壹贰叁肆伍陆柒捌玖拾";
??? String s4="分角整元拾佰仟万拾佰仟亿拾佰仟";
???
??? String temp="";
??? String result="";
???
??? if(money.length()==0||money==null)
????? return "请输入数字字符";
???
??? temp=money.trim();
??? float f;
??? try{
????? f=Float.parseFloat(temp);
??? }catch(NumberFormatException e){
????? return "输入数字";
??? }
???
??? int len=0;
??? if(temp.indexOf(".")==-1){
????? len=temp.length();
??? }else{
????? len=temp.indexOf(".");
??? }
???
??? if(len>s4.length()-3)
????? return? "输入的数字只能输入千亿";
???
??? int n1, n2;
??? String num="";
??? String unit="";
???
??? for(int i=0; i????? if(i>len+2){
??????? break;
????? }
????? if(i==len)
??????? continue;
????? n1=Integer.parseInt(String.valueOf(temp.charAt(i)));
????? num=s1.substring(n1, n1+1);
????? n1=len-i+2;
????? unit=s4.substring(n1, n1+1);
????? result=result.concat(num).concat(unit);
??? }
???
??? if((len==temp.length()||len==temp.length()-1))
????? result=result.concat("整");
??? if(len==temp.length()-2)
????? result=result.concat("零分");
??? return result;
? }
? public static void main(String arg[]){
??? NumToChinese num=new NumToChinese();
??? System.out.println(num.numToChinese("45621237012.56"));
? }
}
此博客展示了一段Java代码,实现将数字转换为中文金额的功能。代码中定义了数字和单位对应的字符串,对输入的数字进行格式检查和长度判断,通过循环将数字和单位拼接成中文金额,最后处理特殊情况并返回结果。
556

被折叠的 条评论
为什么被折叠?



