java实现浮点数转换成人民币读法字符串,希望大家多多指教!

java实现浮点数转换成人民币读法字符串

package crazyit; public class Num2Rmb { private String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"}; private String[] danArr = {"十", "百", "千", "万"}; // //此方法【数组】仅作参考 // private String[] div(double num){ // // long zheng = (long)num; // long part = Math.round((num-zheng)*100); // return new String[]{zheng + " ", String.valueOf(part)}; // // } //读取传进的参数 private String maString(double mnumString) { //将mnumString 强制类型转化为long,取整数部分 long mnumint = (long)mnumString; long part = Math.round((mnumString - mnumint) * 100); String result = ""; String result1 = ""; String test = ""; long temp2 ; int count = 0; //调用小数方法 result1 = partString(part); // 主要思想:利用循环导出后四位 do { count++; temp2 = mnumint % 10000; test = String.valueOf(temp2); if (mnumint == 0) { continue; } switch (count) { case 1: result = fourString(test) + result; break; case 2: //对万到千万是否全部为0情况的判断 if (fourString(test)=="") { result = fourString(test) + result; } else { result = fourString(test) +"万" + result; } break; case 3: result = fourString(test) +"亿" + result; default: break; } mnumint = mnumint / 10000; } while (mnumint != 0); if (part == 0) { return result + "圆整"; } else { return result + "圆" + result1; } } //封装一个每四位一读的方法 private String fourString(String numStr){ String result = ""; int numLen = numStr.length(); for(int i = 0; i < numLen ; i++) { int num = numStr.charAt(i) - 48; if (i !=numLen -1 && num != 0) { result += hanArr[num] + danArr[numLen - 2 -i]; } else { if (num == 0) { //如果是在首位或末位为0则不读 if (i == numLen-1 || i == 0) continue; else if ((numStr.charAt(i+1) - 48) == 0){//如果连续两位是0不读 continue; } } result += hanArr[num]; } } return result; } //封装一个小数的读法 private String partString(long num) { String result = ""; int test; test = (int) (num / 10); result = hanArr[test] + "角"; result += hanArr[(int)(num % 10)] + "分"; return result; } public static void main(String[] args) { // TODO Auto-generated method stub Num2Rmb nRmb = new Num2Rmb(); // System.out.println(Arrays.toString(nRmb.div(2342.342))); System.out.println(nRmb.maString(100000001)); } }

你可能感兴趣的:(java)