用java实现,数字四舍五入时,只小数部分四舍五入,整数不进位

代码
/**
  * 只小数部分四舍五入,整数不进位
  * @param d
  * @return
  */
 public Double checkNumber(Double d){
  String[] strArray = d.toString().replace('.',',').split(",");
  String left = strArray[0];
  String right = strArray[1];
  if(right.length()<3){
   return d;
  }else{
      Character char2 = right.charAt(1);
      Character char3 = right.charAt(2);
      if(char2.equals('9')&&Integer.parseInt(char3.toString())>=5){
          return Double.parseDouble(d.toString().substring(0,d.toString().length()-1));
      }else{
          return Double.parseDouble(formatNumeric(d,0));
      }
  }
 }
// 数字转逗号分隔字符串,附加小数位数(保留8位小数,那么dec参数为6,即,最少要有2位小数)
 public static String formatNumeric(double numeric, int dec) {
  String p 
= "";
  
for (int i = 0; i < dec; i++)
   p 
+= "#";
  
return formatNumeric(numeric, "#,##0.00" + p);
 }
 
// 数字转字符串
 public static String formatNumeric(double numeric, String pattern) {
  
if (numeric == -0)
   numeric 
= 0;
  DecimalFormat decFormat 
= new DecimalFormat(pattern);
  
return decFormat.format(numeric);
 }
原文地址:https://www.cnblogs.com/mabaishui/p/1862672.html