Java精确运算(BigDecimal)

        BigDecimal b1 = new BigDecimal("1.34002");
        BigDecimal b2 = new BigDecimal("1.34");
        String string = b1.add(b2).toString();
        System.out.println(string);  //2.68002

1 import java.math.BigDecimal;  
  2 public class MathExtend  
  3 {  
  4   //默认除法运算精度  
  5   private static final int DEFAULT_DIV_SCALE = 10;  
  6    
  7  /** 
  8   * 提供精确的加法运算。 
  9   * @param v1 
 10   * @param v2 
 11   * @return 两个参数的和 
 12   */  
 13   public static double add(double v1, double v2)  
 14   {  
 15       BigDecimal b1 = new BigDecimal(Double.toString(v1));  
 16       BigDecimal b2 = new BigDecimal(Double.toString(v2));  
 17       return b1.add(b2).doubleValue();  
 18   }  
 19   /** 
 20    * 提供精确的加法运算 
 21    * @param v1   
 22    * @param v2 
 23    * @return 两个参数数学加和,以字符串格式返回 
 24    */  
 25   public static String add(String v1, String v2)  
 26   {  
 27       BigDecimal b1 = new BigDecimal(v1);  
 28       BigDecimal b2 = new BigDecimal(v2);  
 29       return b1.add(b2).toString();  
 30   }  
 31    
 32  /** 
 33   * 提供精确的减法运算。 
 34   * @param v1 
 35   * @param v2 
 36   * @return 两个参数的差 
 37   */  
 38   public static double subtract(double v1, double v2)  
 39   {  
 40       BigDecimal b1 = new BigDecimal(Double.toString(v1));  
 41       BigDecimal b2 = new BigDecimal(Double.toString(v2));  
 42       return b1.subtract(b2).doubleValue();  
 43   }  
 44    
 45   /** 
 46    * 提供精确的减法运算 
 47    * @param v1 
 48    * @param v2 
 49    * @return 两个参数数学差,以字符串格式返回 
 50    */  
 51   public static String subtract(String v1, String v2)  
 52   {  
 53       BigDecimal b1 = new BigDecimal(v1);  
 54       BigDecimal b2 = new BigDecimal(v2);  
 55       return b1.subtract(b2).toString();  
 56   }  
 57    
 58    
 59   /** 
 60   * 提供精确的乘法运算。 
 61   * @param v1 
 62   * @param v2 
 63   * @return 两个参数的积 
 64   */  
 65   public static double multiply(double v1, double v2)  
 66   {  
 67       BigDecimal b1 = new BigDecimal(Double.toString(v1));  
 68       BigDecimal b2 = new BigDecimal(Double.toString(v2));  
 69       return b1.multiply(b2).doubleValue();  
 70   }  
 71    
 72   /** 
 73    * 提供精确的乘法运算 
 74    * @param v1 
 75    * @param v2 
 76    * @return 两个参数的数学积,以字符串格式返回 
 77    */  
 78   public static String multiply(String v1, String v2)  
 79   {  
 80       BigDecimal b1 = new BigDecimal(v1);  
 81       BigDecimal b2 = new BigDecimal(v2);  
 82       return b1.multiply(b2).toString();  
 83   }  
 84    
 85   /** 
 86   * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 
 87   * 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN 
 88   * @param v1 
 89   * @param v2 
 90   * @return 两个参数的商 
 91   */  
 92   public static double divide(double v1, double v2)  
 93   {  
 94       return divide(v1, v2, DEFAULT_DIV_SCALE);  
 95   }  
 96    
 97   /** 
 98    * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 
 99    * 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN 
100    * @param v1 
101    * @param v2 
102    * @param scale 表示需要精确到小数点以后几位。 
103    * @return 两个参数的商 
104    */  
105   public static double divide(double v1,double v2, int scale)  
106   {  
107       return divide(v1, v2, scale, BigDecimal.ROUND_HALF_EVEN);  
108   }  
109    
110   /** 
111    * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 
112    * 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式 
113    * @param v1 
114    * @param v2 
115    * @param scale 表示需要精确到小数点以后几位 
116    * @param round_mode 表示用户指定的舍入模式 
117    * @return 两个参数的商 
118    */  
119   public static double divide(double v1,double v2,int scale, int round_mode){  
120           if(scale < 0)  
121           {  
122               throw new IllegalArgumentException("The scale must be a positive integer or zero");  
123           }  
124           BigDecimal b1 = new BigDecimal(Double.toString(v1));  
125           BigDecimal b2 = new BigDecimal(Double.toString(v2));  
126           return b1.divide(b2, scale, round_mode).doubleValue();  
127   }  
128    
129   /** 
130    * 提供(相对)精确的除法运算,当发生除不尽的情况时,精确到 
131    * 小数点以后10位,以后的数字四舍五入,舍入模式采用ROUND_HALF_EVEN 
132    * @param v1 
133    * @param v2 
134    * @return 两个参数的商,以字符串格式返回 
135    */  
136   public static String divide(String v1, String v2)  
137   {  
138       return divide(v1, v2, DEFAULT_DIV_SCALE);  
139   }  
140    
141   /** 
142    * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 
143    * 定精度,以后的数字四舍五入。舍入模式采用ROUND_HALF_EVEN 
144    * @param v1 
145    * @param v2 
146    * @param scale 表示需要精确到小数点以后几位 
147    * @return 两个参数的商,以字符串格式返回 
148    */  
149   public static String divide(String v1, String v2, int scale)  
150   {  
151       return divide(v1, v2, DEFAULT_DIV_SCALE, BigDecimal.ROUND_HALF_EVEN);  
152   }  
153    
154   /** 
155    * 提供(相对)精确的除法运算。当发生除不尽的情况时,由scale参数指 
156    * 定精度,以后的数字四舍五入。舍入模式采用用户指定舍入模式 
157    * @param v1 
158    * @param v2 
159    * @param scale 表示需要精确到小数点以后几位 
160    * @param round_mode 表示用户指定的舍入模式 
161    * @return 两个参数的商,以字符串格式返回 
162    */  
163   public static String divide(String v1, String v2, int scale, int round_mode)  
164   {  
165       if(scale < 0)  
166       {  
167           throw new IllegalArgumentException("The scale must be a positive integer or zero");  
168       }  
169       BigDecimal b1 = new BigDecimal(v1);  
170       BigDecimal b2 = new BigDecimal(v2);  
171       return b1.divide(b2, scale, round_mode).toString();  
172   }  
173    
174   /** 
175    * 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN 
176    * @param v 需要四舍五入的数字 
177    * @param scale 小数点后保留几位 
178    * @return 四舍五入后的结果 
179    */  
180   public static double round(double v,int scale)  
181   {  
182       return round(v, scale, BigDecimal.ROUND_HALF_EVEN);  
183   }  
184   /** 
185    * 提供精确的小数位四舍五入处理 
186    * @param v 需要四舍五入的数字 
187    * @param scale 小数点后保留几位 
188    * @param round_mode 指定的舍入模式 
189    * @return 四舍五入后的结果 
190    */  
191   public static double round(double v, int scale, int round_mode)  
192   {  
193      if(scale<0)  
194      {  
195          throw new IllegalArgumentException("The scale must be a positive integer or zero");  
196      }  
197      BigDecimal b = new BigDecimal(Double.toString(v));  
198      return b.setScale(scale, round_mode).doubleValue();  
199   }  
200    
201   /** 
202    * 提供精确的小数位四舍五入处理,舍入模式采用ROUND_HALF_EVEN 
203    * @param v 需要四舍五入的数字 
204    * @param scale 小数点后保留几位 
205    * @return 四舍五入后的结果,以字符串格式返回 
206    */  
207   public static String round(String v, int scale)  
208   {  
209     return round(v, scale, BigDecimal.ROUND_HALF_EVEN);  
210   }  
211   /** 
212    * 提供精确的小数位四舍五入处理 
213    * @param v 需要四舍五入的数字 
214    * @param scale 小数点后保留几位 
215    * @param round_mode 指定的舍入模式 
216    * @return 四舍五入后的结果,以字符串格式返回 
217    */  
218   public static String round(String v, int scale, int round_mode)  
219   {  
220      if(scale<0)  
221      {  
222          throw new IllegalArgumentException("The scale must be a positive integer or zero");  
223      }  
224      BigDecimal b = new BigDecimal(v);  
225      return b.setScale(scale, round_mode).toString();  
226   }  
227 }  

猜你喜欢

转载自blog.csdn.net/SecondDream_1017/article/details/81084704