浮点数类型的比较大小

https://www.jianshu.com/p/4679618fd28c  这篇文章介绍的比较仔细。

 

 1 package com.cy.test.math;
 2 
 3 
 4 import java.math.BigDecimal;
 5 
 6 public class TestCompareDouble {
 7 
 8     /**
 9      * 比较两个double类型数的大小
10      * @param args
11      */
12     public static void main(String[] args) {
13         double targetScore = Double.parseDouble("0.5");
14         double score = 0.500001;
15 
16         BigDecimal score1 = new BigDecimal(String.valueOf(targetScore));
17         BigDecimal score2 = new BigDecimal(String.valueOf(score));
18         System.out.println(score2.doubleValue());
19 
20         if(score2.compareTo(score1) >= 0){
21             System.out.println("score2大于等于score1");
22         }
23     }
24 }

 

posted on 2019-12-04 17:09  有点懒惰的大青年  阅读(918)  评论(0编辑  收藏  举报