Adroid浮点数格式化 DecimalFormat

Android中小数位的格式化处理一般用DecimalFormat来搞定。

public DecimalFormat(String pattern)

参数pattern是一个模式化字符串,用来决定数据格式化的规则。
常用的通配符有俩:0, #

0: 整数位:少的左边补0, 多的不处理。 小数位:少的右边补0,多的四舍五入(当然不一定是四舍五入,怎样处理可以自己设置)

#:整数位:少的不处理,多的也不处理。小数位:少的不处理,多的四舍五入(当然不一定是四舍五入,怎样处理可以自己设置) 如果小数位的后边是0,不显示。

        double num  = 120.12415926;
        DecimalFormat df = new DecimalFormat("00.00");
        System.out.println("输出的结果为:"+df.format(num));
        double num1  = 20.120;
        DecimalFormat df1 = new DecimalFormat("000.000");
        System.out.println("输出的结果为:"+df1.format(num1));

        double num2  = 120.12415926;
        DecimalFormat df2 = new DecimalFormat("##.##");
        System.out.println("输出的结果为:"+df2.format(num2));
        double num3  = 20.120;
        DecimalFormat df3 = new DecimalFormat("###.###");
        System.out.println("输出的结果为:"+df3.format(num3));

输出的结果如下:
// I/System.out: 输出的结果为:120.12
// I/System.out: 输出的结果为:020.120
// I/System.out: 输出的结果为:120.12
// I/System.out: 输出的结果为:20.12

结果不精确的舍入行为通过 DecimalFormat的setRoundingMode(RoundingMode roundingMode)来决定。
RoundingMode如下:

public enum RoundingMode {

    /**
     *  若舍入位为非零,则对舍入部分的前一位数字加1;若舍入位为零,则直接舍弃。即为向外取整模式。
     * Rounding mode where positive values are rounded towards positive infinity
     * and negative values towards negative infinity.
     * 
* Rule: {@code x.round().abs() >= x.abs()} */ UP(BigDecimal.ROUND_UP), /** * 不论舍入位是否为零,都直接舍弃。即为向内取整模式。 * Rounding mode where the values are rounded towards zero. *
* Rule: {@code x.round().abs() <= x.abs()} */ DOWN(BigDecimal.ROUND_DOWN), /** * 若 BigDecimal 为正,则舍入行为与 ROUND_UP 相同;若为负,则舍入行为与 ROUND_DOWN 相同。即为向上取整模式 * Rounding mode to round towards positive infinity. For positive values * this rounding mode behaves as {@link #UP}, for negative values as * {@link #DOWN}. *
* Rule: {@code x.round() >= x} */ CEILING(BigDecimal.ROUND_CEILING), /** * 若 BigDecimal 为正,则舍入行为与 ROUND_DOWN 相同;若为负,则舍入行为与 ROUND_UP 相同。即为向下取整模式。 * Rounding mode to round towards negative infinity. For positive values * this rounding mode behaves as {@link #DOWN}, for negative values as * {@link #UP}. *
* Rule: {@code x.round() <= x} */ FLOOR(BigDecimal.ROUND_FLOOR), /** * (四舍五入)若舍入位大于等于5,则对舍入部分的前一位数字加1;若舍入位小于5,则直接舍弃。即为四舍五入模式。 * Rounding mode where values are rounded towards the nearest neighbor. Ties * are broken by rounding up. */ HALF_UP(BigDecimal.ROUND_HALF_UP), /** * 若舍入位大于5,则对舍入部分的前一位数字加1;若舍入位小于等于5,则直接舍弃。即为五舍六入模式。 * Rounding mode where values are rounded towards the nearest neighbor. Ties * are broken by rounding down. */ HALF_DOWN(BigDecimal.ROUND_HALF_DOWN), /** * 若(舍入位大于5)或者(舍入位等于5且前一位为奇数),则对舍入部分的前一位数字加1; * 若(舍入位小于5)或者(舍入位等于5且前一位为偶数),则直接舍弃。即为银行家舍入模式。 * Rounding mode where values are rounded towards the nearest neighbor. Ties * are broken by rounding to the even neighbor. */ HALF_EVEN(BigDecimal.ROUND_HALF_EVEN), /** * 如果数据是精确的,正常; 如果数据不是恰好精确的,就抛异常。 * Rounding mode where the rounding operations throws an ArithmeticException * for the case that rounding is necessary, i.e. for the case that the value * cannot be represented exactly. */ UNNECESSARY(BigDecimal.ROUND_UNNECESSARY);

over~

你可能感兴趣的:(android)