java保留两位小数

1、double保留两位小数

 /**
     * double类型保留两位小数
     * 方法一:format
     */
    public static void doubleKeepTwoDecimalPlaces(){
        double a = 12.34000;
        String data = String.format("%.2f",a);
        System.out.println("方法一(format)结果:"+data);

    }

运行结果:

E:\java\Java\jdk1.8.0_281\bin\java.exe "-javaagent:D:\App\JetBrains\IntelliJ IDEA 2023.1\lib\idea_rt.jar=61543:D:\App\JetBrains\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -classpath E:\java\Java\jdk1.8.0_281\jre\lib\charsets.jar;E:\java\Java\jdk1.8.0_281\jre\lib\deploy.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\dnsns.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\jaccess.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\localedata.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\nashorn.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunec.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\zipfs.jar;E:\java\Java\jdk1.8.0_281\jre\lib\javaws.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jce.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jfr.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jfxswt.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jsse.jar;E:\java\Java\jdk1.8.0_281\jre\lib\management-agent.jar;E:\java\Java\jdk1.8.0_281\jre\lib\plugin.jar;E:\java\Java\jdk1.8.0_281\jre\lib\resources.jar;E:\java\Java\jdk1.8.0_281\jre\lib\rt.jar;E:\javacode\Learning\out\production\Learning;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.7.20\kotlin-stdlib-jdk8-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.7.20\kotlin-stdlib-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.7.20\kotlin-stdlib-common-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.7.20\kotlin-stdlib-jdk7-1.7.20.jar Learning.Time.KeepTwoDecimalPlaces
方法一(format)结果:12.34

进程已结束,退出代码0


2、BigDecimal保留两位小数

 /**
     * Bigdecimal保留两位小数
     * 方法一:setScale
     */
    public static void bigdecimalKeepTwoDecimalPlaces(){
        BigDecimal bigDecimal = new BigDecimal("12.340000");
        BigDecimal a = bigDecimal.setScale(2,BigDecimal.ROUND_HALF_UP);
        System.out.println("方法一(setScale)结果:"+a);
    }

运行结果:

E:\java\Java\jdk1.8.0_281\bin\java.exe "-javaagent:D:\App\JetBrains\IntelliJ IDEA 2023.1\lib\idea_rt.jar=61680:D:\App\JetBrains\IntelliJ IDEA 2023.1\bin" -Dfile.encoding=UTF-8 -classpath E:\java\Java\jdk1.8.0_281\jre\lib\charsets.jar;E:\java\Java\jdk1.8.0_281\jre\lib\deploy.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\access-bridge-64.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\cldrdata.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\dnsns.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\jaccess.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\jfxrt.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\localedata.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\nashorn.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunec.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunjce_provider.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunmscapi.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\sunpkcs11.jar;E:\java\Java\jdk1.8.0_281\jre\lib\ext\zipfs.jar;E:\java\Java\jdk1.8.0_281\jre\lib\javaws.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jce.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jfr.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jfxswt.jar;E:\java\Java\jdk1.8.0_281\jre\lib\jsse.jar;E:\java\Java\jdk1.8.0_281\jre\lib\management-agent.jar;E:\java\Java\jdk1.8.0_281\jre\lib\plugin.jar;E:\java\Java\jdk1.8.0_281\jre\lib\resources.jar;E:\java\Java\jdk1.8.0_281\jre\lib\rt.jar;E:\javacode\Learning\out\production\Learning;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk8\1.7.20\kotlin-stdlib-jdk8-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib\1.7.20\kotlin-stdlib-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-common\1.7.20\kotlin-stdlib-common-1.7.20.jar;C:\Users\13451\.m2\repository\org\jetbrains\annotations\13.0\annotations-13.0.jar;C:\Users\13451\.m2\repository\org\jetbrains\kotlin\kotlin-stdlib-jdk7\1.7.20\kotlin-stdlib-jdk7-1.7.20.jar Learning.Time.KeepTwoDecimalPlaces
方法一(setScale)结果:12.34

进程已结束,退出代码0

你可能感兴趣的:(Java,java,intellij-idea,jvm)