java 两个整数相除保留两位小数

1 第一种

import java.math.BigDecimal;

/**
 * @author WGR
 * @create 2020/3/17 -- 15:51
 */
public class DemoTest {

    public static void main(String[] args) {
        int a=100;
        int b=33;
        double f1 = new BigDecimal((float)a/b).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        System.out.println(f1);
    }
}

 2 第二种

import java.text.DecimalFormat;

/**
 * @author WGR
 * @create 2020/3/17 -- 15:51
 */
public class DemoTest {

    public static void main(String[] args) {
        int a=100;
        int b=33;
        DecimalFormat df = new DecimalFormat("0.00");//格式化小数
        String num = df.format((float)a/b);//返回的是String类型
        System.out.println(num);
    }
}

评论

h
hw_7864

⭐️ Admin快速开发: https://show.cool-admin.com/

赞 0
h
hw_7864

⭐️ 官方文档: https://cool-js.com

赞 0
h
hw_7864

⭐️ chatgpt在线体验: https://cool-js.com/ai/chat-vip/index.html?r=helloworld

赞 0