|
@@ -0,0 +1,128 @@
|
|
|
+package com.gyee.backconfig.util.realtimedate;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author fhq
|
|
|
+ *
|
|
|
+ */
|
|
|
+public class MathUtil {
|
|
|
+
|
|
|
+ // 截取小数点后两位
|
|
|
+ public static Double twoBit(Double dd) {
|
|
|
+ if (dd == null) {
|
|
|
+ return dd;
|
|
|
+ } else {
|
|
|
+ BigDecimal decimal = new BigDecimal(dd);
|
|
|
+ decimal = decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
|
|
|
+ return decimal.doubleValue();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化小数位数,四舍五入
|
|
|
+ *
|
|
|
+ * @param dd
|
|
|
+ * @param Scale
|
|
|
+ * 小数位数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double decimal(Double dd, int Scale) {
|
|
|
+ return new BigDecimal(dd).setScale(Scale, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化小数位数,四舍五入
|
|
|
+ *
|
|
|
+ * @param dd
|
|
|
+ * @param Scale
|
|
|
+ * 小数位数
|
|
|
+ * @return 无格式字符串 ,末位去零
|
|
|
+ */
|
|
|
+ public static String plain(Double dd, int Scale) {
|
|
|
+ if (dd == 0) {
|
|
|
+ return "0";
|
|
|
+ } else {
|
|
|
+ return new BigDecimal(dd).setScale(Scale, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .stripTrailingZeros().toPlainString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转Double
|
|
|
+ *
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Double getDouble(String value) {
|
|
|
+ return new BigDecimal(value).doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化小数位数,四舍五入
|
|
|
+ *
|
|
|
+ * @param dd
|
|
|
+ * @param Scale
|
|
|
+ * 小数位数
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Float decimal(Float dd, int Scale) {
|
|
|
+ return new BigDecimal(dd).setScale(Scale, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .floatValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 格式化小数位数,四舍五入
|
|
|
+ *
|
|
|
+ * @param dd
|
|
|
+ * @param Scale
|
|
|
+ * 小数位数
|
|
|
+ * @return 无格式字符串
|
|
|
+ */
|
|
|
+ public static String plain(Float dd, int Scale) {
|
|
|
+ return new BigDecimal(dd).setScale(Scale, BigDecimal.ROUND_HALF_UP)
|
|
|
+ .toPlainString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 字符串转Float
|
|
|
+ *
|
|
|
+ * @param value
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Float getFloat(String value) {
|
|
|
+ return new BigDecimal(value).floatValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将10进制数转换成二进制数
|
|
|
+ */
|
|
|
+ public static String getBinaryString(Double value) {
|
|
|
+ return Integer.toBinaryString(value.intValue());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 在指定时间上加指定的小时
|
|
|
+ * @param date
|
|
|
+ * @param hour
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static Date addHours(Date date, double hour) {
|
|
|
+ return new Date(date.getTime() + (long) (hour * 3600 * 1000));
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Integer toInt(String str) {
|
|
|
+ return Integer.parseInt(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Float toFloat(String str) {
|
|
|
+ return Float.parseFloat(str);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static Double toDouble(String str) {
|
|
|
+ return Double.parseDouble(str);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|