diff --git a/fastbee-common/src/main/java/com/fastbee/common/utils/NumberUtils.java b/fastbee-common/src/main/java/com/fastbee/common/utils/NumberUtils.java new file mode 100644 index 0000000..93e847a --- /dev/null +++ b/fastbee-common/src/main/java/com/fastbee/common/utils/NumberUtils.java @@ -0,0 +1,84 @@ +package com.fastbee.common.utils; + +import java.text.DecimalFormat; + +/** + * 数字工具类 + */ +public class NumberUtils { + + /** + * 保留两位小数,不足补0 + * @param number + * @return + */ + public static float formatFloat(float number) { + return (float) (Math.round(number * 100) / 100.0); + } + + /** + * 保留两位小数,不足补0 + * @param number + * @return + */ + public static Double formatDouble(Double number) { + return (Double) (Math.round(number * 100) / 100.0); + } + + /** + * 字符串类型保留两位小数,不足补0 + * @param str + * @return + */ + public static String formatString(String str) { + if (isNumeric(str)) { + float number = Float.parseFloat(str); + DecimalFormat df = new DecimalFormat("0.00"); + return df.format(number); + } else { + return str; + } + } + + /** + * 字符串类型保留三位小数,不足补0 + * @param str + * @return + */ + public static String formatThreeString(String str) { + if (isNumeric(str)) { + float number = Float.parseFloat(str); + DecimalFormat df = new DecimalFormat("0.000"); + return df.format(number); + } else { + return str; + } + } + + /** + * 字符串类型保留三位小数,不足补0 + * @param str + * @param format 样式例如0.000 + * @return + */ + public static String formatStringByFormat(String str,String format) { + if (isNumeric(str)) { + float number = Float.parseFloat(str); + DecimalFormat df = new DecimalFormat(format); + return df.format(number); + } else { + return str; + } + } + + /** + * 正则判断是否为数字 + * @param str + * @return + */ + public static boolean isNumeric(String str) { + return str.matches("-?\\d+(\\.\\d+)?"); + } + + +} diff --git a/fastbee-service/fastbee-waterele-service/src/main/java/com/fastbee/waterele/service/impl/MaGuangaiRecordServiceImpl.java b/fastbee-service/fastbee-waterele-service/src/main/java/com/fastbee/waterele/service/impl/MaGuangaiRecordServiceImpl.java index a133249..4b10e9b 100644 --- a/fastbee-service/fastbee-waterele-service/src/main/java/com/fastbee/waterele/service/impl/MaGuangaiRecordServiceImpl.java +++ b/fastbee-service/fastbee-waterele-service/src/main/java/com/fastbee/waterele/service/impl/MaGuangaiRecordServiceImpl.java @@ -10,6 +10,7 @@ import cn.hutool.http.HttpUtil; import cn.hutool.json.JSON; import cn.hutool.json.JSONArray; import cn.hutool.json.JSONObject; +import com.alibaba.excel.util.NumberUtils; import com.fastbee.common.core.page.PageDomain; import com.fastbee.common.core.page.TableDataInfo; import com.fastbee.common.core.page.TableSupport; @@ -117,7 +118,7 @@ public class MaGuangaiRecordServiceImpl implements IMaGuangaiRecordService record.setCurEle(item.get("cur_ele").toString()); record.setCardId(item.get("card_id").toString()); record.setAreaCode(item.get("area_code").toString()); - record.setUserBalance(item.get("user_balance").toString()); + record.setUserBalance(com.fastbee.common.utils.NumberUtils.formatFloat(Float.parseFloat(item.get("user_balance").toString())/100)+""); record.setCurFlow(item.get("cur_flow").toString()); record.setCurEle(item.get("cur_ele").toString()); record.setStatus(Integer.parseInt(item.get("status").toString()));