From d1f26741bddf6f512d62c0100d42c52be8d37e76 Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期六, 06 二月 2021 15:35:40 +0800 Subject: [PATCH] 工具类优化 --- utils/src/main/java/org/yeshi/utils/NumberUtil.java | 200 +++++++++++++++++++++++++------------------------- 1 files changed, 100 insertions(+), 100 deletions(-) diff --git a/utils/src/main/java/org/yeshi/utils/NumberUtil.java b/utils/src/main/java/org/yeshi/utils/NumberUtil.java index 10ff871..56ab71e 100644 --- a/utils/src/main/java/org/yeshi/utils/NumberUtil.java +++ b/utils/src/main/java/org/yeshi/utils/NumberUtil.java @@ -1,100 +1,100 @@ -package org.yeshi.utils; - -import java.text.DecimalFormat; -import java.util.regex.Pattern; - -public class NumberUtil { - - /** - * 淇濈暀2浣嶅皬鏁� - * - * @param price - * @return - */ - public static String get2PointNumber(double price) { - DecimalFormat decimalFormat = new DecimalFormat("##0.00");// - String p = decimalFormat.format(price);// - return p; - } - - /** - * 淇濈暀1浣嶅皬鏁� - * - * @param price - * @return - */ - public static String get1PointNumber(float price) { - DecimalFormat decimalFormat = new DecimalFormat("##0.0"); - String p = decimalFormat.format(price); - return p; - } - - /** - * 淇濈暀1浣嶅皬鏁� - * - * @param price - * @return - */ - public static String get1PointNumber(double price) { - DecimalFormat decimalFormat = new DecimalFormat("##0.0");// 鏋勶拷?鏂规硶鐨勫瓧绗︽牸寮忚繖閲屽鏋滃皬鏁颁笉锟�?锟�?浼氫互0琛ヨ冻. - String p = decimalFormat.format(price);// format 杩斿洖鐨勬槸瀛楃锟�? - return p; - } - - private static String getNumberWithZero(int number) { - if (number < 10) - return "0" + number; - else - return number + ""; - } - - /** - * 灏嗙杞负鏃堕棿瀛楃涓� - * - * @param second - * @return - */ - public static String convertSecondToString(int second) { - if (second < 60) {// 灏忎簬1鍒嗛挓 - return "00:" + getNumberWithZero(second); - } else if (second < 60 * 60) {// 灏忎簬1灏忔椂 - int minite = second / 60; - return getNumberWithZero(minite) + ":" + getNumberWithZero(second % 60); - } else { - int hour = second / (60 * 60); - int minite = (second % (60 * 60)) / 60; - int se = (second % (60 * 60)) % 60; - return getNumberWithZero(hour) + ":" + getNumberWithZero(minite) + ":" + getNumberWithZero(se); - } - } - - /** - * 鍒ゆ柇瀛楃涓蹭腑鏄惁鍖呭惈鏁板瓧 - * - * @param str - * @return - */ - public static boolean isNumeric(String str) { - - if (str == null || "".equals(str.trim())) { - return false; - } - - Pattern pattern = Pattern.compile("[0-9]*"); - return pattern.matcher(str).matches(); - } - - /** - * 鍘婚櫎灏忔暟鐐瑰悗闈� 澶氫綑鐨�0 - * - * @param s - * @return - */ - public static String subZeroAndDot(String s) { - if (s.indexOf(".") > 0) { - s = s.replaceAll("0+?$", "");// 鍘绘帀澶氫綑0 - s = s.replaceAll("[.]$", "");// 濡傛渶鍚庝竴浣嶆槸.鍒欏幓0 - } - return s; - } -} +package org.yeshi.utils; + +import java.text.DecimalFormat; +import java.util.regex.Pattern; + +public class NumberUtil { + + /** + * 淇濈暀2浣嶅皬鏁� + * + * @param price + * @return + */ + public static String get2PointNumber(double price) { + DecimalFormat decimalFormat = new DecimalFormat("##0.00");// + String p = decimalFormat.format(price);// + return p; + } + + /** + * 淇濈暀1浣嶅皬鏁� + * + * @param price + * @return + */ + public static String get1PointNumber(float price) { + DecimalFormat decimalFormat = new DecimalFormat("##0.0"); + String p = decimalFormat.format(price); + return p; + } + + /** + * 淇濈暀1浣嶅皬鏁� + * + * @param price + * @return + */ + public static String get1PointNumber(double price) { + DecimalFormat decimalFormat = new DecimalFormat("##0.0");// 鏋勶拷?鏂规硶鐨勫瓧绗︽牸寮忚繖閲屽鏋滃皬鏁颁笉锟�?锟�?浼氫互0琛ヨ冻. + String p = decimalFormat.format(price);// format 杩斿洖鐨勬槸瀛楃锟�? + return p; + } + + private static String getNumberWithZero(int number) { + if (number < 10) + return "0" + number; + else + return number + ""; + } + + /** + * 灏嗙杞负鏃堕棿瀛楃涓� + * + * @param second + * @return + */ + public static String convertSecondToString(int second) { + if (second < 60) {// 灏忎簬1鍒嗛挓 + return "00:" + getNumberWithZero(second); + } else if (second < 60 * 60) {// 灏忎簬1灏忔椂 + int minite = second / 60; + return getNumberWithZero(minite) + ":" + getNumberWithZero(second % 60); + } else { + int hour = second / (60 * 60); + int minite = (second % (60 * 60)) / 60; + int se = (second % (60 * 60)) % 60; + return getNumberWithZero(hour) + ":" + getNumberWithZero(minite) + ":" + getNumberWithZero(se); + } + } + + /** + * 鍒ゆ柇瀛楃涓蹭腑鏄惁鍖呭惈鏁板瓧 + * + * @param str + * @return + */ + public static boolean isNumeric(String str) { + + if (str == null || "".equals(str.trim())) { + return false; + } + + Pattern pattern = Pattern.compile("[0-9]*"); + return pattern.matcher(str).matches(); + } + + /** + * 鍘婚櫎灏忔暟鐐瑰悗闈� 澶氫綑鐨�0 + * + * @param s + * @return + */ + public static String subZeroAndDot(String s) { + if (s.indexOf(".") > 0) { + s = s.replaceAll("0+?$", "");// 鍘绘帀澶氫綑0 + s = s.replaceAll("[.]$", "");// 濡傛渶鍚庝竴浣嶆槸.鍒欏幓0 + } + return s; + } +} -- Gitblit v1.8.0