概述
最近用到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 FilesJavajdk1.8.0_231binjava.exe" "-javaagent:E:program filesJetBrainsIntelliJ IDEA 2019.1.3libidea_rt.jar=58898:E:program filesJetBrainsIntelliJ IDEA 2019.1.3bin" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJavajdk1.8.0_231jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_231jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_231jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_231jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_231jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_231jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_231jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_231jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_231jrelibextnashorn.jar;C:Program FilesJavajdk1.8.0_231jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_231jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_231jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_231jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_231jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_231jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_231jrelibjce.jar;C:Program FilesJavajdk1.8.0_231jrelibjfr.jar;C:Program FilesJavajdk1.8.0_231jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_231jrelibjsse.jar;C:Program FilesJavajdk1.8.0_231jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_231jrelibplugin.jar;C:Program FilesJavajdk1.8.0_231jrelibresources.jar;C:Program FilesJavajdk1.8.0_231jrelibrt.jar;E:NewWorldworkworkspaceclassesproductionQianQian" TestDecimal
movePointRight:21
multiply:38
scaleByPowerOfTen:5
scaleByPowerOfTen > movePointRight > multiply
scaleByPowerOfTen 性能最佳,但是输出的是 科学计算法:9.9E+3
使用 toPlainString()可以原样输出:
//输出 9900
System.out.println(b1.toPlainString());
但是这样处理后性能不及 movePointRight
movePointRight > multiply > scaleByPowerOfTen
最后
以上就是危机绿茶为你收集整理的BigDecimal乘以100的几种方法 及性能测试的全部内容,希望文章能够帮你解决BigDecimal乘以100的几种方法 及性能测试所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复