import java.math.BigDecimal;
import java.text.DecimalFormat;

import com.mysql.jdbc.StringUtils;

public class NumberFormatUtilEx {
    private static String[] units = { "", "", "", "", "", "十万", "百万", "千万", "亿", "十亿", "百亿", "千亿", "万亿" };
    private static char[] numArray = { '', '', '', '', '', '', '', '', '', '' };

    public static String formatZh(int num) {
        char[] val = String.valueOf(num).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String m = val[i] + "";
            int n = Integer.valueOf(m);
            boolean isZero = n == 0;
            String unit = units[(len - 1) - i];
            if (isZero) {
                if ('0' == val[i - 1]) {
                    // not need process if the last digital bits is 0
                    continue;
                } else {
                    // no unit for 0
                    sb.append(numArray[n]);
                }
            } else {
                sb.append(numArray[n]);
                sb.append(unit);
            }
        }
        return sb.toString();
    }

    /**
     * 格式化数字为两位
     * 
     * @param number
     * @return
     */
    public static String format(BigDecimal number) {
        if (number == null) {
            return "";
        }
        DecimalFormat df = new DecimalFormat("#,##0.00");
        return df.format(number);
    }
    
    /**
     * 格式化数字为两位
     * 
     * @param number
     * @return
     */
    public static String formatNoZero(BigDecimal number) {
        if (number == null) {
            return "";
        }
        DecimalFormat df = new DecimalFormat("#,##0.##");
        return df.format(number);
    }
    
    /**
     *     去掉小数点后面多余的0
     */
 
    public static String subZeroAndDot(String s){    
        if(s.indexOf(".") > 0){    
            s = s.replaceAll("0+?$", "");//去掉多余的0    
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉    
        }    
        return s;    
    }  

    /**
     * 格式化数字为两位
     * 
     * @param Str
     * @return
     */
    public static String format(String Str) {
        if (StringUtils.isNullOrEmpty(Str)) {
            return "";
        }
        BigDecimal number = new BigDecimal(Str);
        DecimalFormat df = new DecimalFormat("#,##0.00");
        return df.format(number);
    }
    
    /**
     * 格式化数字为两位
     * 
     * @param Str
     * @return
     */
    public static String formatNoZero(String Str) {
        if (StringUtils.isNullOrEmpty(Str)) {
            return "";
        }
        BigDecimal number = new BigDecimal(Str);
        DecimalFormat df = new DecimalFormat("#,##0.##");
        return df.format(number);
    }
    
    /**
     * 格式化数字为四位,单位为 万
     * 
     * @param Str
     * @return
     */
    public static String formatFour(String Str) {
        if (StringUtils.isNullOrEmpty(Str)) {
            return "";
        }
        BigDecimal number = new BigDecimal(Str).divide(new BigDecimal(10000));
        if(number.compareTo(new BigDecimal(0)) < 0){
            number = number.multiply(new BigDecimal(-1));
        }
        DecimalFormat df = new DecimalFormat("#,##0.00");
        return df.format(number);
    }

    public static void main(String args[]) {
        System.out.println(NumberFormatUtilEx.formatNoZero("10000000.00"));
    }

 

posted on 2017-07-31 11:16  怂人不倦  阅读(427)  评论(0)    收藏  举报