背景: 项目上有个需求,需要计算比例两位小数的百分数,然后按照百分数进行高低排序  约定是两位小数

 

做法: 先获取到String类型的小数 比如说  32.21,然后 使用replace函数 把“.”替换到“”  不够的小数位数补0  然后将替换后的字符串转换为 int在进行比较

public static void main(String[] args) {
   String simi = "32.21";//代码计算的
   Map<String, Object> sp = new HashMap<>();
   if (!CommonUtil.isStringEmptyOrNull(simi)){
      simi = simi.replace(".","");
      if (4 > simi.length()){
         int s = 4 - simi.length();
         for (int j = 0; j < s; j++){
            simi = simi+"0";
         }
      }
      Integer i = Integer.parseInt(simi);
      if (i>=80*100){
         sp.put("simi", "1");//极高
      }else if (80*100>i && i>=50*100){
         sp.put("simi", "2");//较高
      }else if(50*100>i && i>=20*100){
         sp.put("simi", "3");//一般
      }else if (20*100>i && i>0){
         sp.put("simi", "4");//较低
      }
   }
}