|
@@ -0,0 +1,46 @@
|
|
|
+package com.platform.common.util;
|
|
|
+
|
|
|
+import org.apache.poi.ss.usermodel.Cell;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.text.DecimalFormat;
|
|
|
+
|
|
|
+import static cn.hutool.core.util.StrUtil.EMPTY;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Description 字符串工具类
|
|
|
+ * @Author chenli
|
|
|
+ * @Date 2019/7/23
|
|
|
+ * @Version Copyright (c) 2019,北京乾元坤和科技有限公司 All rights reserved.
|
|
|
+ */
|
|
|
+public class MathUtils {
|
|
|
+
|
|
|
+ /**
|
|
|
+ *发现个奇怪的问题,这个方法的四舍五入不算真正的四舍五入,只要后面一位有值就会往前进一位,
|
|
|
+ *例如:123.3333取两位小数之后是123.34。
|
|
|
+ */
|
|
|
+ public static double formatDouble2(double d,int num){
|
|
|
+ //旧的方法,不推荐使用
|
|
|
+// BigDecimal bd = new BigDecimal(d).setScale(2,BigDecimal.ROUND_HALF_UP);
|
|
|
+ //新的方法,如果不需要四舍五入可以使用RoundingMode.DOWN
|
|
|
+ BigDecimal bd = new BigDecimal(d).setScale(num, RoundingMode.UP);
|
|
|
+ return bd.doubleValue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String formatDouble1(double f,int num) {
|
|
|
+ //#.00 表示两位小数
|
|
|
+ String str = "#0.";
|
|
|
+ for (int i=0;i<num;i++){
|
|
|
+ str+=0;
|
|
|
+ }
|
|
|
+ DecimalFormat df = new DecimalFormat(str);
|
|
|
+ return df.format(f);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ formatDouble1(1.0000,2);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|