|
|
@@ -1,5 +1,9 @@
|
|
|
package com.uas.platform.b2b.support;
|
|
|
|
|
|
+import com.uas.platform.core.exception.IllegalOperatorException;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
|
/**
|
|
|
@@ -19,14 +23,25 @@ public class DecimalUtils {
|
|
|
* @param digit 小数点
|
|
|
* @return 返回处理后的数据
|
|
|
*/
|
|
|
- public static Double decimalPoint(Double oldValue, Integer digit) {
|
|
|
+ public static double decimalPoint(Double oldValue, Integer digit) {
|
|
|
if (null != oldValue) {
|
|
|
- String str = "#.###############";
|
|
|
- String digitStr = str.substring(0, (2 + digit));
|
|
|
- DecimalFormat decimalFormat = new java.text.DecimalFormat(digitStr);
|
|
|
- return Double.valueOf(decimalFormat.format(oldValue));
|
|
|
- } else {
|
|
|
- return 0.00;
|
|
|
+ BigDecimal decimal = new BigDecimal(oldValue);
|
|
|
+ return decimal.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
|
|
|
+ }
|
|
|
+ return 0.00;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 传入double数据,根据需要保留的位数,四舍五入原则。
|
|
|
+ */
|
|
|
+ public static Double fractionNumHalfUp(Double value, Integer fraction) {
|
|
|
+ if (value == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (fraction == null || fraction < 0) {
|
|
|
+ throw new IllegalOperatorException("您传入的需要保留的有效位数是" + fraction + ",请核对信息");
|
|
|
}
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(String.valueOf(value));
|
|
|
+ return bigDecimal.setScale(fraction, RoundingMode.HALF_UP).doubleValue();
|
|
|
}
|
|
|
}
|