java高效的获取指定的精度的double数

 1 package com.czp;
 2 
 3 
 4 public class MathUtil {
 5 
 6     public static void main(String[] args){
 7         double d = 123.5465893;
 8         System.out.println(getRealVaule(d, 0));
 9         //=>124
10         System.out.println(getRealVaule(d, 3));
11         //=>123.547
12 
13     }
14     /**
15      * 对结果进行四舍五入
16      * 
17      * @param value  :原始数
18      * @param resLen :所要的精度
19      * @return
20      */
21     public static   Number getRealVaule(double value,int resLen) {
22         if(resLen==0)
23             //原理:123.456*10=1234.56+5=1239.56/10=123
24             //原理:123.556*10=1235.56+5=1240.56/10=124
25             return Math.round(value*10+5)/10;
26         double db  = Math.pow(10, resLen);
27         return Math.round(value*db)/db;
28     }
29 }
原文地址:https://www.cnblogs.com/czpblog/p/2865293.html