3.使用Decimel进行小数的操作

public class TestBigDecimel extends test implements B {

    public static void main(String[] args) {
        System.out.println(B.a);
        System.out.println(b);

        double f = 1111.5585;
        BigDecimal bigDecimal = new BigDecimal(f);

        /*实现保留位,方法1*/
        double f1 = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
        double f2 = bigDecimal.setScale(2,BigDecimal.ROUND_UP).doubleValue();
        double f3 = bigDecimal.setScale(2,BigDecimal.ROUND_DOWN).doubleValue();

        System.out.println("f1 : "+ f1+"| f2 : "+ f2+" | f3 : "+ f3);

        /*实现保留位,方法二*/
        /* #.00 表示两位小数 #.0000四位小数 以此类推…*/
        java.text.DecimalFormat format = new java.text.DecimalFormat("#.00");
        String re = format.format(f);

        System.out.println("re : "+ re);

        /*实现保留位,方法三*/
        /* %.2f
        "%." 表示 小数点前任意位数
        "2" 表示两位小数
        格式后的结果为"f" 表示浮点型。*/
        String result = String.format("%.2f",f);
        System.out.println("result :"+ result);


    }




}

interface A{

    int a = 0;
//    void f();
}
interface B extends A {

}

class test{
    public static int b = 2;
}

输出

0
2
f1 : 1111.56| f2 : 1111.56 | f3 : 1111.55
re : 1111.56
result :1111.56

猜你喜欢

转载自blog.csdn.net/changshuchao/article/details/88406003