v1.4.1
Parker 4 years ago
parent eccac41dd9
commit 226fca1314

@ -0,0 +1,99 @@
/**
* Copyright 2020 OPSLI https://www.opsli.com
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.opsli.common.utils;
import java.math.BigDecimal;
/**
* @BelongsProject: opsli-boot
* @BelongsPackage: org.opsli.common.utils
* @Author: Parker
* @CreateTime: 2020-09-19 23:21
* @Description:
*/
public class BigDecimalUtil {
/**
* add
* @param value1
* @param value2
* @return
*/
public static double add(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.toString(value1));
BigDecimal b2 = new BigDecimal(Double.toString(value2));
return b1.add(b2).doubleValue();
}
/**
* sub
* @param value1
* @param value2
* @return
*/
public static double sub(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.toString(value1));
BigDecimal b2 = new BigDecimal(Double.toString(value2));
return b1.subtract(b2).doubleValue();
}
/**
* mul
* @param value1
* @param value2
* @return
*/
public static double mul(double value1,double value2){
BigDecimal b1 = new BigDecimal(Double.toString(value1));
BigDecimal b2 = new BigDecimal(Double.toString(value2));
return b1.multiply(b2).doubleValue();
}
/**
* div
* @param value1
* @param value2
* @param scale
* @param model
* 0 ROUND_UP 1
* 1 ROUND_DOWN 1
* 2 ROUND_CEILING .UP .DOWN
* 3 ROUND_FLOOR .DOWN .UP
* 4 ROUND_HALF_UP >= 0.5 .UP .DOWN
* 5 ROUND_HALF_DOWN > 0.5 .UP .DOWN
* 6 ROUND_HALF_EVEN .HALF_UP .HALF_DOWN
* 7 ROUND_UNNECESSARY ArithmeticException
* @return
* @throws IllegalAccessException
*/
public static double div(double value1,double value2,int scale,int model) throws IllegalAccessException{
//如果精确范围小于0抛出异常信息
if(scale<0){
throw new IllegalAccessException("精确度不能小于0");
}
BigDecimal b1 = new BigDecimal(Double.toString(value1));
BigDecimal b2 = new BigDecimal(Double.toString(value2));
return b1.divide(b2, scale, model).doubleValue();
}
public static double div(double value1,double value2,int scale) throws IllegalAccessException{
//如果精确范围小于0抛出异常信息
if(scale<0){
throw new IllegalAccessException("精确度不能小于0");
}
BigDecimal b1 = new BigDecimal(Double.toString(value1));
BigDecimal b2 = new BigDecimal(Double.toString(value2));
return b1.divide(b2, scale,4).doubleValue();
}
}
Loading…
Cancel
Save