• 用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);
     }
  • 相关阅读:
    C++ XML文件解析
    coco2d-x create tableView
    cocos2d-x button setTitleLabel
    cocos2d-X create std colorlayer
    Windows cmd "tree"
    C/C++ std::function && std::bind
    C/C++ “std::invoke”: 未找到匹配的重载函数
    31 迭代器 Iterator 是什么?
    30 哪些集合类是线程安全的?
    02 rpx 与 px
  • 原文地址:https://www.cnblogs.com/mabaishui/p/1862672.html
Copyright © 2020-2023  润新知