人民幣大寫轉換實現 – JAVA編程語言程序開發技術文章

Java代碼 
public class RmbCapitalFormatter { 
     
    //大寫對應數組 
    private static final String RMB_CAPITAL[] = { 
            "零", "壹", "貳", "叁", "肆", "伍", "陸", "柒", "捌", "玖", "拾" 
    }; 
      
    private static final HashMap<String, Integer> RMB_CAPITAL_MAP = new HashMap<String, Integer>(); 
    static { 
        RMB_CAPITAL_MAP.put("零", 0); 
        RMB_CAPITAL_MAP.put("壹", 1); 
        RMB_CAPITAL_MAP.put("貳", 2); 
        RMB_CAPITAL_MAP.put("叁", 3); 
        RMB_CAPITAL_MAP.put("肆", 4); 
        RMB_CAPITAL_MAP.put("伍", 5); 
        RMB_CAPITAL_MAP.put("陸", 6); 
        RMB_CAPITAL_MAP.put("柒", 7); 
        RMB_CAPITAL_MAP.put("捌", 8); 
        RMB_CAPITAL_MAP.put("玖", 9); 
        RMB_CAPITAL_MAP.put("拾", 10); 
    } 
     
    //是圓(復)還是元(簡) 
    private boolean isSimpleYuan = false; 
     
    //是整(復)還是正(簡) 
    private boolean isSimpleZheng = false; 
 
    public RmbCapitalFormatter() { 
    } 
 
    public boolean isSimpleYuan() { 
        return isSimpleYuan; 
    } 
 
    public void setSimpleYuan(boolean isSimpleYuan) { 
        this.isSimpleYuan = isSimpleYuan; 
    } 
 
    public boolean isSimpleZheng() { 
        return isSimpleZheng; 
    } 
 
    public void setSimpleZheng(boolean isSimpleZheng) { 
        this.isSimpleZheng = isSimpleZheng; 
    } 
 
    public String format(Double data) { 
        StringBuilder buf = new StringBuilder(); 
        boolean isNegative = false; 
        if (data < 0) { 
            isNegative = true; 
            data = 0 – data; 
        } 
        //整數部分 
        long intPart = data.longValue(); 
        //小數部分,隻有前兩位小數有意義 
        int fractionPart = (int)((data – intPart) * 100); 
        if (intPart == 0 && fractionPart == 0) { 
            return "人民幣零圓整"; 
        } 
         
        String cur = "";         
        boolean needZero = false; 
        //億為一個大循環,四位數是一個小循環 
        while(intPart > 0) { 
            int lowestFourDigits = (int)(intPart % 10000); 
            intPart = intPart / 10000; 
             
            if (lowestFourDigits > 0) { 
                if (needZero) { 
                    buf.insert(0, "零"); 
                    needZero = false; 
                } 
                buf.insert(0, changeFourDigists(lowestFourDigits) + cur); 
                if (lowestFourDigits < 1000) { 
                    needZero = true; 
                } 
            } else { 
                if ("億".equals(cur)) { 
                    buf.insert(0, cur); 
                } 
            } 
            if (cur.length() == 0) { 
                cur = "萬"; 
            } else if ("萬".equals(cur)) { 
                cur = "億"; 
            }  else if ("億".equals(cur)) { 
                cur = "萬"; 
            } 
        } 
        if (data > 1) { 
            if (isSimpleYuan) { 
                buf.append("元"); 
            } else { 
                buf.append("圓"); 
            } 
        }        
        if (fractionPart > 0) { 
            int jiaoPart = fractionPart / 10; 
            int fenPart = fractionPart % 10; 
            if (jiaoPart > 0) { 
                buf.append(RMB_CAPITAL[jiaoPart]).append("角"); 
            } 
            if (fenPart > 0) { 
                if (data > 1 && jiaoPart == 0) { 
                    buf.append(RMB_CAPITAL[0]);// 零 
                } 
                buf.append(RMB_CAPITAL[fenPart]).append("分"); 
            } 
        } else { 
            if (isSimpleZheng) { 
                buf.append("正"); 
            } else { 
                buf.append("整"); 
            } 
        } 
        String result = buf.toString(); 
        if (isNegative) { 
            return "人民幣負" + result; 
        } else { 
            return "人民幣" + result; 
        } 
    } 
 
    //四位整數轉換成大寫金額 
    private static String changeFourDigists(int number) { 
        if (number == 0) { 
            return "零"; 
        } 
         
        StringBuilder buf = new StringBuilder(); 
        //千位數部分 
        int qianPart = number / 1000; 
        number = number % 1000; 
        //百位數部分 
        int baiPart = number / 100; 
        number = number % 100; 
        //十位數部分 
        int shiPart = number / 10; 
        number = number % 10; 
        //個位數部分 
        int gePart = number; 
         
        //是否需要添零 
        boolean needZero = false; 
        //前面是否有值 
        boolean havaValueBefore = false; 
        if (qianPart > 0) { 
            buf.append(RMB_CAPITAL[qianPart]).append("仟"); 
            havaValueBefore = true; 
        } else { 
            havaValueBefore = false; 
        } 
 
        if (baiPart > 0) { 
            buf.append(RMB_CAPITAL[baiPart]).append("佰"); 
            havaValueBefore = true; 
        } else { 
            if (havaValueBefore) { 
                needZero = true; 
            } 
        } 
 
        if (shiPart > 0) { 
            if (needZero) { 
                buf.append("零"); 
                needZero = false; 
            } 
            buf.append(RMB_CAPITAL[shiPart]).append("拾"); 
            havaValueBefore = true; 
        }  else { 
            if (havaValueBefore) { 
                needZero = true; 
            } 
        } 
         
        if (gePart > 0) { 
            if (needZero) { 
                buf.append("零"); 
                needZero = false; 
            } 
            buf.append(RMB_CAPITAL[gePart]); 
        } 
         
        return buf.toString(); 
    } 
 
    public Double unformat(String data) { 
        boolean isNegative = false; 
        if (data.startsWith("人民幣")) { 
            data = data.substring(3); 
        } 
        if (data.startsWith("負")) { 
            isNegative = true; 
            data = data.substring(1); 
        } 
        //整數部分 
        long intPart = 0; 
        //小數部分 
        int fractionPart = 0; 
         
        //千分位部分 
        int qianPart = 0; 
        //百分位部分 
        int baiPart = 0; 
        //十分位部分 
        int shiPart = 0; 
        //個位部分 
        int gePart = 0; 
        while(data.length() > 0) { 
            String token = data.substring(0, 1); 
             
            if (token.equals("零")) { 
            } else if (token.equals("整") || token.equals("正")) { 
                break; 
            } else if (token.equals("角")) { 
                shiPart = gePart * 10; 
                gePart = 0; 
            } else if (token.equals("分")) { 
                fractionPart += shiPart + gePart; 
                shiPart = 0; 
                gePart = 0; 
                break; 
            } else if (token.equals("圓") || token.equals("元")) { 
                intPart += qianPart + baiPart + shiPart + gePart; 
                qianPart = 0; 
                baiPart = 0; 
                shiPart = 0; 
                gePart = 0; 
            } else if (token.equals("拾")) { 
                shiPart = gePart * 10; 
                gePart = 0; 
            } else if (token.equals("佰")) { 
                baiPart = gePart * 100; 
                gePart = 0; 
            } else if (token.equals("仟")) { 
                qianPart = gePart * 1000; 
                gePart = 0;          
            } else if (token.equals("萬")) { 
                intPart += (qianPart + baiPart + shiPart + gePart) * 10000; 
                qianPart = 0; 
                baiPart = 0; 
                shiPart = 0; 
                gePart = 0; 
            } else if (token.equals("億")) { 
                intPart += qianPart + baiPart + shiPart + gePart; 
                intPart = intPart * 100000000; 
                qianPart = 0; 
                baiPart = 0; 
                shiPart = 0; 
                gePart = 0;              
            } else { 
                gePart = RMB_CAPITAL_MAP.get(token); 
            } 
            data = data.substring(1);            
        } 
        double result = intPart + ((double)fractionPart) / 100; 
        if (isNegative) { 
            return 0 – result; 
        } else { 
            return result; 
        } 
    } 

作者“wuyuhou”
 

發佈留言