|
@@ -0,0 +1,101 @@
|
|
|
|
+package com.winhc.bigdata.spark.utils
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author: XuJiakai
|
|
|
|
+ * @date: 2021/8/13 16:24
|
|
|
|
+ */
|
|
|
|
+object BigDecimalUtil {
|
|
|
|
+ // 进行加法运算
|
|
|
|
+ def add(v1: String, v2: String): Double = {
|
|
|
|
+ val b1 = new BigDecimal(v1)
|
|
|
|
+ val b2 = new BigDecimal(v2)
|
|
|
|
+ b1.add(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def add(v1: Double, v2: Double): Double = {
|
|
|
|
+ val b1 = new BigDecimal(v1)
|
|
|
|
+ val b2 = new BigDecimal(v2)
|
|
|
|
+ b1.add(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 进行减法运算
|
|
|
|
+ def subtract(v1: String, v2: String): Double = {
|
|
|
|
+ val b1 = new BigDecimal(v1)
|
|
|
|
+ val b2 = new BigDecimal(v2)
|
|
|
|
+ b1.subtract(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def subtract(v1: Double, v2: Double): Double = {
|
|
|
|
+ val b1 = new BigDecimal(v1)
|
|
|
|
+ val b2 = new BigDecimal(v2)
|
|
|
|
+ b1.subtract(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 进行乘法运算
|
|
|
|
+ def mul(d1: String, d2: String): Double = {
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ b1.multiply(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def mul(d1: Double, d2: Double): Double = {
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ b1.multiply(b2).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 进行除法运算
|
|
|
|
+ def div(d1: String, d2: String, len: Int): Double = {
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ if (b2.doubleValue == 0) return 0
|
|
|
|
+ b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def div(d1: Double, d2: Double, len: Int): Double = {
|
|
|
|
+ if (d2 == 0) return 0
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def div(d1: Long, d2: Long, len: Int): Double = {
|
|
|
|
+ if (d2 == 0) return 0
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 进行四舍五入操作-len 小数点后保留几位
|
|
|
|
+ def round(d: Double, len: Int): Double = {
|
|
|
|
+ val b1 = new BigDecimal(d)
|
|
|
|
+ val b2 = new BigDecimal(1)
|
|
|
|
+ // 任何一个数字除以1都是原数字
|
|
|
|
+ // ROUND_HALF_UP是BigDecimal的一个常量,表示进行四舍五入的操作
|
|
|
|
+ b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 进行四舍五入操作 -并保留指定位数
|
|
|
|
+ *
|
|
|
|
+ * @param @ param d1 处理数据
|
|
|
|
+ * @param @ param d2 如果为1 则自己保留多少位,100为除以100之后的结果再保留位数
|
|
|
|
+ * @param @ param len 小数点保留位数
|
|
|
|
+ * @param @ return 参数
|
|
|
|
+ * @return String 返回类型
|
|
|
|
+ * @throws
|
|
|
|
+ * @Title: round
|
|
|
|
+ * @Description:
|
|
|
|
+ */
|
|
|
|
+ def round(d1: String, d2: String, len: Int): Double = {
|
|
|
|
+ val b1 = new BigDecimal(d1)
|
|
|
|
+ val b2 = new BigDecimal(d2)
|
|
|
|
+ val res = b1.divide(b2, len, BigDecimal.ROUND_HALF_UP).doubleValue
|
|
|
|
+ res
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ def double2Long(d: Double): Long = d.round
|
|
|
|
+}
|