您的位置:首页 > 编程语言 > Java开发

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

2014-06-25 10:05 441 查看
public class NumberToRMB {

    private final String[] hanArr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};

    private final String[] unitArr = {"仟", "", "十", "佰"};

    private final String[] tag = {"元", "万", "亿"};

    

    public String toHanStr(double num) {

        

        String result = "";

        

        long zheng = (long)num;

        //long xiao = Math.round((num - zheng) * 100);//如果需要四舍五入,就用这个方法,但是小数点后

                                                      //两位依次为99时,第三位不能>=5,否则会报错

        long xiao = (long)((num - zheng) * 100);//如果不需要四舍五入,就用这个方法

        

        /*整数部分为0单独考虑*/

        if(0 == zheng) {

            

            int tempJiao = (int)(xiao / 10);

            int tempFen = (int)(xiao % 10);

            

            if(tempJiao != 0) {

                result += hanArr[tempJiao] + "角";

            }

            if(tempFen != 0) {

                result += hanArr[tempFen] + "分";

            }

            

            return result;

        }

        

        String zhengStr = String.valueOf(zheng);

        int len = zhengStr.length();

        

        int tempLen = 0;

        

        /*处理整数部分*/

        for(int i = 0; i < len; i++) {

            

            int temp = zhengStr.charAt(i) - 48;

            int part = (len - i - 1) / 4; //当前字符处于哪个段

            int location = (len - i - 1) % 4; //当前字符处于该段的具体哪个位置

            

            if(location != 0) { //不是该段的最后一个

                

                if(temp != 0) { //当前字符不为0

                    result += hanArr[temp] + unitArr[(len - i) % 4]; continue;

                } else { //当前字符为0

                    tempLen = result.length();

                    if(3 == location && result.charAt(tempLen - 1) != '零') { //当前字符为0且为该段第一个

                        result += "零"; continue;

                    } else {

                        tempLen = result.length();

                        if(result.charAt(tempLen - 1) == '零') {

                            continue;

                        } else {

                            result += "零"; continue;

                        }

                    }

                }

            } else { //是该段最后一个

                if(temp != 0) {

                    result += hanArr[temp] + tag[part]; continue;

                } else {

                    tempLen = result.length();

                    if(result.charAt(tempLen - 1) != '零') {

                        result += tag[part]; continue;

                    } else {

                        if(result.charAt(tempLen - 2) == '亿' || result.charAt(tempLen - 2) == '万') {

                            continue;

                        } else {

                            result = result.substring(0, tempLen - 1) + tag[part]; continue;

                        }

                    }

                }

            }

            

        }

        

        /*处理小数部分*/

        int jiao = (int)(xiao / 10);

        int fen = (int)(xiao % 10);

        

        if(jiao != 0) {

            result += hanArr[jiao] + "角";

        }

        if(fen != 0) {

            result += hanArr[fen] + "分";

        }

        

        /*排除输入形式如60 0000 0000.00,输出为“陆十亿零”的情况*/

        tempLen = result.length();

        if(result.charAt(tempLen - 1) == '零') {

            result = result.substring(0, tempLen - 1);

        }

        tempLen = result.length();

        if(result.charAt(tempLen - 1) == '元') {

            result += "整";

        }

        if(result.charAt(tempLen - 1) == '万' || result.charAt(tempLen - 1) == '亿') {

            result += "元整";

        }

        

        return result;

    }

    public static void main(String[] args) {

        

    NumberToRMB ntr = new NumberToRMB();

        System.out.println(ntr.toHanStr(6000000000.0000));

        System.out.println(ntr.toHanStr(600100.000));

        System.out.println(ntr.toHanStr(1006.333));

        System.out.println(ntr.toHanStr(10.111));

    }
}

部分算法如下:

       int len = zhengStr.length();//取整数部分字符串长度

       for(i = 0; i < len; i++)//遍历整个整数字符串

           int temp = zhengStr.charAt(i) - 48;//当前字符转为数值

           int part = (len - i - 1) / 4;//当前字符位于那一段

           int location = (len - i - 1) % 4;//当前字符处于段的那一个位置

           if 当前字符不是本段最后一个

               if 当前字符不为‘0’    result += 转换成的繁体字 + 单位;    continue;

               else 当前字符为‘0’

                   if 当前字符是该段第一个字符并且result串的最后一个字符!='零' result += '零';continue;

                   else

                       if result串的最后一个字符为'零'    continue;

                       else result串的最后一个字符不为'零'    result += '零';

           else 当前字符是本段最后一个

               if 当前字符不为‘0’    result += 转换成的繁体字 + 段单位;    continue;

               else 当前字符为'0'

                   if result串的最后一个字符 != '零'    result += 段单位;    continue;

                   else resutl串的最后一个字符 == '零'

                       if result串的倒数第二个字符为'亿'或者‘万’    continue;

                       else result串的倒数第二个字符不为'亿'或者'万'  result+=result串去零+段单位;continue;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: