BigDecimal乘以100的几种方法 及性能测试

最近用到decimal计算要乘100

//修改主观题得分率为:主观题得分/总分   修改客观题得分率为:客观题得分/总分
if (Objects.nonNull(exam.getTotalScore()) && exam.getTotalScore().compareTo(BigDecimal.ZERO) != 0) {
    userExamReviewList.setSubjectiveScoreRate(userExamReviewDTO.getSubjectiveScore().multiply(new BigDecimal(100)).divide(exam.getTotalScore(), 2, ROUND_HALF_UP).floatValue());
    userExamReviewList.setCorrectRate(userExamReviewDTO.getObjectiveScore().multiply(new BigDecimal(100)).divide(exam.getTotalScore(), 2, ROUND_HALF_UP).floatValue());
}
 
  

有没有优雅一点的处理方式呢?以及他们的性能怎么样?

import java.math.BigDecimal;

/**
 * @Title: BigDecimal乘以100的几种方法 及性能测试
 * @Author: ken
 * @Description:
 * @Date: 2021/7/21  9:32
 * @Param:
 * @return
 **/
public class TestDecimal {
    public static void main(String[] args) {
        BigDecimal b1 = new BigDecimal(0);
        //movePointRight
        long start = System.currentTimeMillis();
        for(int i = 0; i <10000000; i ++){
            BigDecimal d = new BigDecimal(99);
            b1 = d.movePointRight(2);
/*            if(i >90 && i<= 100){
                System.out.println(b1);
            }*/
        }
        long end = System.currentTimeMillis();
        System.out.println("movePointRight:" + (end -start) );
        //multiply
        start = System.currentTimeMillis();
        for(int i = 0; i <10000000; i ++){
            BigDecimal d = new BigDecimal(99);
            b1 = d.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN);
/*            if(i >90 && i<= 100){
                System.out.println(b1);
            }*/
        }
        end = System.currentTimeMillis();
        System.out.println("multiply:" + (end -start) );
        //scaleByPowerOfTen
        start = System.currentTimeMillis();
        for(int i = 0; i <10000000; i ++){
            BigDecimal d = new BigDecimal(99);
            b1 = d.scaleByPowerOfTen(2);
            b1.toPlainString();
            /*if(i >90 && i<= 100){
                System.out.println(b1);
                System.out.println(b1.toPlainString());
            }*/
        }
        end = System.currentTimeMillis();
        System.out.println("scaleByPowerOfTen:" + (end -start) );

    }
}

看看他们的性能:​​​​

"C:\Program Files\Java\jdk1.8.0_231\bin\java.exe" "-javaagent:E:\program files\JetBrains\IntelliJ IDEA 2019.1.3\lib\idea_rt.jar=58898:E:\program files\JetBrains\IntelliJ IDEA 2019.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_231\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_231\jre\lib\rt.jar;E:\NewWorld\work\workspace\classes\production\QianQian" TestDecimalmovePointRight:21multiply:38scaleByPowerOfTen:5

scaleByPowerOfTen > movePointRight > multiply

scaleByPowerOfTen 性能最佳,但是输出的是 科学计算法:9.9E+3

使用 toPlainString()可以原样输出:

//输出 9900

System.out.println(b1.toPlainString());

但是这样处理后性能不及 movePointRight

movePointRight > multiply > scaleByPowerOfTen

BigDecimal乘以100的几种方法 及性能测试_第1张图片

你可能感兴趣的:(java,java)