JAVA—咖啡馆

——欢迎访问rogerfan的博客,常来《JAVA——咖啡馆》坐坐,喝杯浓香的咖啡,彼此探讨一下JAVA技术,交流工作经验,分享JAVA带来的快乐!本网站部分转载文章,如果有版权问题请与我联系。

BlogJava 首页 新随笔 联系 聚合 管理
  447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks
  1public class NumberHelper {   
  2  
  3    private static Log logger = LogFactory.getLog(NumberHelper.class);   
  4       
  5    /**  
  6     * 将源数字格式化为指定的长度,不足的位数在前面补零,如果长度小于源数字的长度返回原始数字  
  7     * 适用于在产生指定长度的流水号时使用  
  8     *   
  9     * @param source  
 10     * @return  
 11     */
  
 12    public static String number2char(int source, int length) {   
 13        try{   
 14            String module = new String();   
 15            for (int i=0; i<length; i++){   
 16                module += "0";   
 17            }
   
 18            DecimalFormat format = new DecimalFormat(module);   
 19            return format.format(source);   
 20        }
 catch (Exception e){   
 21            logger.error("源数字或指定长度非法", e);   
 22            e.printStackTrace();   
 23            return null;   
 24        }
   
 25    }
   
 26       
 27    /**  
 28     * 将百分比格式化为数字表示,默认使用四舍五入的算法不舍去小数位数  
 29     *   
 30     * @param source 百分比格式的数字  
 31     * @return  
 32     */
  
 33    public static Double percent2number(double source){   
 34        try{   
 35            BigDecimal big = new BigDecimal(source);   
 36            return big.divide(new BigDecimal(100)).doubleValue();   
 37        }
 catch (Exception e){   
 38            logger.error("将百分比格式化为数字表示出现错误", e);   
 39            e.printStackTrace();   
 40            return null;   
 41        }
   
 42    }
   
 43       
 44    /**  
 45     * 将百分比格式化为数字表示,默认使用四舍五入的算法并保留指定位数的小数  
 46     *   
 47     * @param source 百分比格式的数字  
 48     * @param length 小数位数  
 49     * @return  
 50     */
  
 51    public static double percent2number(double source, int length){   
 52        try{   
 53            BigDecimal big = new BigDecimal(source);   
 54            return big.divide(new BigDecimal(100), length, RoundingMode.HALF_UP).doubleValue();   
 55        }
 catch (Exception e){   
 56            logger.error("将百分比格式化为数字表示出现错误", e);   
 57            e.printStackTrace();   
 58            return 0;   
 59        }
   
 60    }
   
 61       
 62    /**  
 63     * 将两个数字相除并返回百分格式  
 64     *   
 65     * @param dividend 被除数  
 66     * @param divisor 除数  
 67     * @return 带%号的字符串  
 68     */
  
 69    public static String divide2Percent(int dividend, int divisor){   
 70        return number2percent(divide(dividend, divisor));   
 71    }
   
 72       
 73    /**  
 74     * 将数字格式化为百分比表示  
 75     *   
 76     * @param source  
 77     * @return  
 78     */
  
 79    public static String number2percent(double source){   
 80        try{   
 81            DecimalFormat format = new DecimalFormat("#.##%");   
 82            return format.format(source);   
 83        }
 catch (Exception e){   
 84            logger.error("将数字格式化为百分比表示出现错误", e);   
 85            e.printStackTrace();   
 86            return null;   
 87        }
   
 88    }
   
 89       
 90    /**  
 91     * 两个整数相除保留两位小数  
 92     *   
 93     * @param dividend 被除数  
 94     * @param divisor 除数  
 95     * @return  
 96     */
  
 97    public static Double divide(int dividend, int divisor){   
 98        return divide(dividend, divisor, 2);   
 99    }
   
100       
101    /**  
102     * 两个整数相除保留指定位数的小数  
103     *   
104     * @param dividend 被除数  
105     * @param divisor 除数  
106     * @param length 保留小数位数的长度  
107     * @return  
108     */
  
109    public static Double divide(int dividend, int divisor, int length){   
110        try{   
111            if (dividend == 0){   
112                return 0D;   
113            }
   
114            BigDecimal big = new BigDecimal(divisor);   
115            double d = big.divide(new BigDecimal(dividend), length, RoundingMode.HALF_UP).doubleValue();   
116            return d;   
117        }
 catch (Exception e){   
118            logger.error("数字相除发生错误", e);   
119            e.printStackTrace();   
120            return null;   
121        }
   
122    }
   
123  
124    /**  
125     * 将double数据保留指定位数的小数  
126     * @param length 保留小数位数的长度  
127     * @return  
128     */
  
129    public static Double divide(double source, int length){   
130        try{   
131            if (source == 0){   
132                return 0D;   
133            }
   
134            BigDecimal big = new BigDecimal(source);   
135            double d = big.setScale(length, RoundingMode.HALF_UP).doubleValue();   
136            return d;   
137        }
 catch (Exception e){   
138            logger.error("保留指定位数的小数发生错误", e);   
139            e.printStackTrace();   
140            return null;   
141        }
   
142    }
   
143       
144    /**  
145     * 将double数据保留指定位数的小数  
146     * @param length 保留小数位数的长度,小数位数不够,以0补全。  
147     * 支持科学计算法  
148     * @return  
149     */
  
150    public static String divideToString(double source, int length){   
151        BigDecimal big = new BigDecimal(source);   
152        return  big.setScale(length, RoundingMode.HALF_UP).toString();   
153    }
   
154       
155    /**  
156     * 将String数据保留指定位数的小数  
157     * @param length 保留小数位数的长度,小数位数不够,以0补全。  
158     * 支持科学计算法  
159     * @return  
160     */
  
161    public static String divideToString(String source, int length){   
162        BigDecimal big = new BigDecimal(source);   
163        return  big.setScale(length, RoundingMode.HALF_UP).toString();   
164    }
   
165       
166       
167    public static void main(String[] args){   
168        System.out.print(divideToString("1.23E4",5));   
169    }
   
170}
  
171
posted on 2010-06-21 10:11 rogerfan 阅读(276) 评论(0)  编辑  收藏 所属分类: 【Java知识】

只有注册用户登录后才能发表评论。


网站导航: