In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you how to use Java mathematical tool class MathUtil, the content is very detailed, interested friends can refer to, hope to be helpful to you.
For everyone to share the specific code of the Java mathematical tool class MathUtil, for your reference, the specific contents are as follows
Package cn.xbz.util.math; import java.math.BigDecimal; / * * @ title Mathematical Computing tool Class * @ description provides commonly used numerical addition, subtraction, multiplication and division calculations, as well as the sum, average, maximum and minimum calculation of multiple values * / public class XMathUtil {/ * default division accuracy * / private static final int DEF_DIV_SCALE = 2 / * * exact addition operation * @ param v1 is added * @ param v2 addition * @ return the sum of the two parameters (BigDecimal) * / public static BigDecimal add (BigDecimal v1, BigDecimal v2) {if (null = = v1) {v1 = BigDecimal.ZERO;} if (null = = v2) {v2 = BigDecimal.ZERO;} return v1.add (v2) } / * exact subtraction operation * @ param v1 subtraction * @ param v2 subtraction * @ return difference between two parameters (BigDecimal) * / public static BigDecimal subtract (BigDecimal v1, BigDecimal v2) {if (null = = v1) {v1 = BigDecimal.ZERO;} if (null = = v2) {v2 = BigDecimal.ZERO;} return v1.subtract (v2) } / * exact multiplication * @ param v1 multiplier * @ param v2 multiplier * @ return Product of two parameters (BigDecimal) * / public static BigDecimal multiply (BigDecimal v1, BigDecimal v2) {if (null = = v1) {v1 = BigDecimal.ONE;} if (null = = v2) {v2 = BigDecimal.ONE;} return v1.multiply (v2) } / * (relative) precise division operation, which is accurate to 2 digits after the decimal point when an inexhaustible division occurs, and then the quotient (BigDecimal) / public static BigDecimal pide (BigDecimal v1, BigDecimal v2) {return v1.pide (v2, DEF_DIV_SCALE, BigDecimal.ROUND_HALF_UP) of the two parameters * @ param v1 divisor * @ param v2 divisor * @ return } / * (relative) precise division operation. When the division is not complete, the precision is specified by the scale parameter, and the subsequent number rounding * @ param v1 divisor * @ param v2 divisor * @ param scale indicates the quotient (BigDecimal) / public static BigDecimal pide (BigDecimal v1, BigDecimal v2, Integer scale) {if (null = = v1) {return BigDecimal.ZERO that needs to be accurate to the two parameters of * @ return after the decimal point. } if (null = = v2) {v2 = BigDecimal.ONE;} if (v2.compareTo (BigDecimal.ZERO) = = 0) {throw new IllegalArgumentException ("Divisor cannot be 0");} if (scale
< 0) { throw new IllegalArgumentException("精确度不能小于0"); } return v1.pide(v2, scale, BigDecimal.ROUND_HALF_UP); } /** * 精确加法运算 * @param v1 被加数 * @param v2 加数 * @return 两个参数的和(String) */ public static String add(String v1, String v2) { if (isBlank(v1)) { v1 = "0"; } if (isBlank(v2)) { v2 = "0"; } BigDecimal b1 = new BigDecimal(v1.trim()); BigDecimal b2 = new BigDecimal(v2.trim()); return String.valueOf(add(b1, b2)); } /** * 精确减法运算 * @param v1 被减数 * @param v2 减数 * @return 两个参数的差(String) */ public static String subtract(String v1, String v2) { if (isBlank(v1)) { v1 = "0"; } if (isBlank(v2)) { v2 = "0"; } BigDecimal b1 = new BigDecimal(v1.trim()); BigDecimal b2 = new BigDecimal(v2.trim()); return String.valueOf(subtract(b1, b2)); } /** * 精确乘法运算 * @param v1 被乘数 * @param v2 乘数 * @return 两个参数的积(String) */ public static String multiply(String v1, String v2) { if (isBlank(v1)) { v1 = "1"; } if (isBlank(v2)) { v2 = "1"; } BigDecimal b1 = new BigDecimal(v1.trim()); BigDecimal b2 = new BigDecimal(v2.trim()); return String.valueOf(multiply(b1, b2)); } /** * ( 相对 )精确除法运算 , 当发生除不尽情况时 , 精确到 小数点以后2位 , 以后数字四舍五入 * @param v1 被除数 * @param v2 除数 * @return 两个参数的商(String) */ public static String pide(String v1, String v2) { return pide(v1, v2, DEF_DIV_SCALE); } /** * ( 相对 )精确除法运算 . 当发生除不尽情况时 , 由scale参数指 定精度 , 以后数字四舍五入 * @param v1 被除数 * @param v2 除数 * @param scale 表示表示需要精确到小数点以后几位 * @return 两个参数的商(String) */ public static String pide(String v1, String v2, Integer scale) { if (null == v1) { return "0"; } if (null == v2) { v2 = "1"; } BigDecimal b1 = new BigDecimal(v1.trim()); BigDecimal b2 = new BigDecimal(v2.trim()); return String.valueOf(pide(b1, b2, scale)); } /** * 精确加法运算 , 计算多个数值总和 , 若其中有null值则忽略 * @param valList 被加数集合 * @return 两个参数的和(BigDecimal) */ public static BigDecimal sum(BigDecimal v1, BigDecimal... valList) { if (null == v1) { v1 = BigDecimal.ZERO; } if (null == valList || valList.length == 0) { return v1; } for (BigDecimal val : valList) { if (null != val) { v1 = v1.add(val); } } return v1; } /** * 精确加法运算 , 计算多个数值总和 , 若其中有null值则忽略 * @param valList 被加数集合 * @return 两个参数的和(String) */ public static String sum(String v1, String... valList) { if (isBlank(v1)) { v1 = "0"; } if (null == valList || valList.length == 0) { return v1; } BigDecimal b1 = new BigDecimal(v1.trim()); for (String val : valList) { if (!isBlank(val)) { b1 = add(b1, new BigDecimal(val.trim())); } } return String.valueOf(b1); } /** * 平均数 * @param valList * @return */ public static BigDecimal avg(BigDecimal... valList) { if (null != valList && valList.length != 0) { return pide(sum(BigDecimal.ZERO, valList), new BigDecimal(valList.length)); } return BigDecimal.ZERO; } /** * 平均数 * @param valList * @return */ public static String avg(String... valList) { if (null != valList && valList.length != 0) { return pide(sum("0", valList), String.valueOf(valList.length)); } return "0"; } /** * 最大值 * @param v1 * @param valList * @return */ public static BigDecimal max(BigDecimal v1, BigDecimal... valList) { BigDecimal max = v1; if (null == valList || valList.length == 0) { return max; } for (BigDecimal val : valList) { if (null != val && val.compareTo(max) >0) {max = val;}} return max;} / * * maximum * @ param valList * @ return * / public static BigDecimal maxArr (BigDecimal... ValList) {if (null = = valList | | valList.length = = 0) {return null;} return max (valList [0], valList);} / * * minimum * @ param v1 * @ param valList * @ return * / public static BigDecimal min (BigDecimal v1, BigDecimal... ValList) {BigDecimal min = v1; if (null = = valList | | valList.length = = 0) {return min;} for (BigDecimal val: valList) {if (null! = val & & val.compareTo (min))
< 0) { min = val; } } return min; } /** * 最小值 * @param valList * @return */ public static BigDecimal minArr(BigDecimal... valList) { if (null == valList || valList.length == 0) { return null; } return min(valList[0], valList); } /** * 最大值 * @param v1 * @param valList * @return */ public static String max(String v1, String... valList) { if (isBlank(v1)) { return null; } if (null == valList || valList.length == 0) { return v1; } BigDecimal maxBd = new BigDecimal(v1.trim()); for (String val : valList) { if (!isBlank(val) && new BigDecimal(val).compareTo(maxBd) >0) {maxBd = new BigDecimal (val);}} return String.valueOf (maxBd);} / * maximum * @ param valList * @ return * / public static String maxArr (String... ValList) {if (null = = valList | | valList.length = = 0) {return null;} return max (valList [0], valList);} / * * minimum * @ param v1 * @ param valList * @ return * / public static String min (String v1, String... ValList) {if (isBlank (v1)) {return null;} if (null = = valList | | valList.length = = 0) {return v1;} BigDecimal minBd = new BigDecimal (v1.trim ()); for (String val: valList) {if (! isBlank (val) & & new BigDecimal (val) .compareto (minBd) < 0) {minBd = new BigDecimal (val);}} return String.valueOf (minBd) } / * minimum * @ param valList * @ return * / public static String minArr (String... ValList) {if (null = = valList | | valList.length = = 0) {return null;} return min (valList [0], valList);} / * determine whether the string is empty (independent of third parties) * @ param str * @ return * / private static boolean isBlank (String str) {return null = = str | | str.trim (). Length () = = 0;}}
About how to use Java mathematical tools class MathUtil to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.