格式化数字为小数点两位(四舍五入)

来源:互联网 发布:java短信开发网关 编辑:程序博客网 时间:2024/06/11 15:46

import java.text.DecimalFormat;

public class NumberUtils {    /**      * @Title: getNum4Cent      * @Description: 将元为单位的转化为分为单位并且四舍五入      * @param @param d      * @param @return      * @return int      * @throws      */    public static int getNum4Cent(double d){        BigDecimal b = new BigDecimal(d * 100);        int i=(int)(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());        return i;    }    /**      * @Title: getNum4Yuan      * @Description:       * @param @param d      * @param @return      * @return double      * @throws      */    public static double getTwoDecimalPlaces(double d){        BigDecimal b = new BigDecimal(d);        double i=(b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());        return i;    }    /**     * 格式化数字为小数点两位(四舍五入)     * @param num     * @return Double     * */    public static  Double getFormatNumber(Double num){        DecimalFormat   df   = new DecimalFormat("######0.00");          return Double.parseDouble(df.format(num));    }}
1 0
原创粉丝点击