zoukankan      html  css  js  c++  java
  • double保留两位小数

        public static String format(double dValue, int lScale) {
            // ////负数,则装化为正数后进行四舍五入
            boolean bFlag = false;
            if (dValue < 0) {
                bFlag = true;
                dValue = -dValue;
            }
            long lPrecision = 10000; // 精度值,为了避免double型出现偏差,增加校验位
            long l45Value = lPrecision / 2 - 1; // 四舍五入的判断值
            long lLength = 1; // 乘的数字
            for (int i = 0; i < lScale; i++) {
                lLength = lLength * 10; // 比如保留2位,乘以100
            }
            long lValue = (long) dValue; // 值的整数部分
            long lValue1 = (long) ((dValue - lValue) * lLength); // 乘以保留位数
            long lValue2 = (long) ((dValue - lValue) * lLength * lPrecision); //
            long lLastValue = lValue2 - (lValue2 / lPrecision) * lPrecision;
            if (lLastValue >= l45Value) {
                lValue1++;
            }
            dValue = lValue + (double) lValue1 / lLength; // 四舍五入后的值
            if (bFlag) {
                dValue = -dValue;
            }
            java.math.BigDecimal bd = new java.math.BigDecimal(dValue);
            bd = bd.setScale(lScale, java.math.BigDecimal.ROUND_HALF_UP);
            return bd.toString();
            // Replace by Huang Ye
            // double d = UtilOperation.Arith.round(dValue, lScale);
            // return String.valueOf(dValue);
        }
  • 相关阅读:
    Threaten Model
    什么是虚拟主机
    http代理服务器
    什么是https
    缓存的实现原理
    Cookie和Session
    HTTP协议详解
    心路历程——毕设程序mr跑不通的问题
    bash: hadoop:command not found
    Mapreduce 测试自带实例 wordcount
  • 原文地址:https://www.cnblogs.com/lifusen/p/7603252.html
Copyright © 2011-2022 走看看