From 220da5159c5ce32f9096e02316b4c9019b09dba7 Mon Sep 17 00:00:00 2001 From: fhx <308050795@qq.com> Date: 星期一, 06 十一月 2023 17:27:15 +0800 Subject: [PATCH] 1.新增工具类 --- src/main/java/com/hx/util/NumberUtil.java | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/hx/util/NumberUtil.java b/src/main/java/com/hx/util/NumberUtil.java index 708ac35..59b9581 100644 --- a/src/main/java/com/hx/util/NumberUtil.java +++ b/src/main/java/com/hx/util/NumberUtil.java @@ -3,6 +3,8 @@ import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * 鏁板瓧宸ュ叿 @@ -337,4 +339,48 @@ return str; } + /**Double绫诲瀷杞寲 + * @param data 鏁版嵁 + * @param digitNumber 淇濈暀澶氬皯浣嶅皬鏁� + * @return + */ + public static Double getTypeDouble(Double data,Integer digitNumber) { + //鍒╃敤BigDecimal鏉ュ疄鐜板洓鑸嶄簲鍏�.淇濈暀涓�浣嶅皬鏁� + double result2 = new BigDecimal(data).setScale(digitNumber, BigDecimal.ROUND_HALF_UP).doubleValue(); + //1浠h〃淇濈暀1浣嶅皬鏁�,淇濈暀涓や綅灏忔暟灏辨槸2 + //BigDecimal.ROUND_HALF_UP 浠h〃浣跨敤鍥涜垗浜斿叆鐨勬柟寮� + + return result2; + } + + /** + * 浠庡瓧绗︿覆涓彁鍙栧皬鏁� + * @param str + * @return + */ + public static String getNumber(String str){ + // 鎺у埗姝e垯琛ㄨ揪寮忕殑鍖归厤琛屼负鐨勫弬鏁�(灏忔暟) + Pattern p = Pattern.compile("(\\d+\\.\\d+)"); + //Matcher绫荤殑鏋勯�犳柟娉曚篃鏄鏈夌殑,涓嶈兘闅忔剰鍒涘缓,鍙兘閫氳繃Pattern.matcher(CharSequence input)鏂规硶寰楀埌璇ョ被鐨勫疄渚�. + Matcher m = p.matcher(str); + //m.find鐢ㄦ潵鍒ゆ柇璇ュ瓧绗︿覆涓槸鍚﹀惈鏈変笌"(\\d+\\.\\d+)"鐩稿尮閰嶇殑瀛愪覆 + if (m.find()) { + //濡傛灉鏈夌浉鍖归厤鐨�,鍒欏垽鏂槸鍚︿负null鎿嶄綔 + //group()涓殑鍙傛暟锛�0琛ㄧず鍖归厤鏁翠釜姝e垯锛�1琛ㄧず鍖归厤绗竴涓嫭鍙风殑姝e垯,2琛ㄧず鍖归厤绗簩涓鍒�,鍦ㄨ繖鍙湁涓�涓嫭鍙�,鍗�1鍜�0鏄竴鏍风殑 + str = m.group(1) == null ? "" : m.group(1); + } else { + //濡傛灉鍖归厤涓嶅埌灏忔暟锛屽氨杩涜鏁存暟鍖归厤 + p = Pattern.compile("(\\d+)"); + m = p.matcher(str); + if (m.find()) { + //濡傛灉鏈夋暣鏁扮浉鍖归厤 + str = m.group(1) == null ? "" : m.group(1); + } else { + //濡傛灉娌℃湁灏忔暟鍜屾暣鏁扮浉鍖归厤,鍗冲瓧绗︿覆涓病鏈夋暣鏁板拰灏忔暟锛屽氨璁句负绌� + str = ""; + } + } + return str; + } + } -- Gitblit v1.8.0