Java 中 Long 类型数据相除,结果保留两位小数

调用 Long 类型的 doubleValue() 方法,以及 String.format() 方法即可


public class LongFormatPercent {
    
    
    public static void main(String[] args) {
    
    
        Long a = 5L;
        Long b = 15L;
        String percentFormat = String.format("%.2f", ((a.doubleValue() / b.doubleValue()) * 100)) + "%";
        System.out.println(percentFormat);
    }
}

运行结果如下:

33.33%

猜你喜欢

转载自blog.csdn.net/m0_37899908/article/details/131904732