1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| { public static String[] han={"零","壹","贰","叁","肆","伍","陆","柒","捌","玖"}; public static String[] unit={"元","十","百","千"}; public static String[] xunit={"角","分"}; public static String[] divide(double num) { long zheng = (long)num; long xiao = Math.round((num-zheng)*100); if(xiao!=0) { return new String[]{zheng+"",xiao+""}; } else { return new String[]{zheng+"",null}; } } public static String toHan(double number) { String[] arr=Convert.divide(number); String numStr=arr[0]; String xiaoStr=arr[1]; String result=""; int numlen=numStr.length(); for (int i=0;i<numlen;i++) { int num=numStr.charAt(i)-48; if(num!=0) { result+=han[num]+unit[numlen-1-i]; } else { result+=han[num]; } } if(xiaoStr!=null) { for (int i=0;i<2;i++) { int num=xiaoStr.charAt(i)-48; result+=han[num]+xunit[i]; } } return result; }
public static void main(String[] args) { System.out.println(toHan(6521.1)); } }
|