- 浏览: 6518 次
-
文章分类
最新评论
数学运算辅助类,希望对大家有所帮助
import java.math.BigDecimal; /** * 数学运算辅助类。 * * @author Elfy * @date 2013年03月21日 */ public class MathUtil { /** * 功能:将字符串转换为BigDecimal,一般用于数字运算时。 * * @author Elfy * @date 2013年03月21日 * @param str * 字符串 * @return BigDecimal,str为empty时返回null。 */ public static BigDecimal toBigDecimal(String str) { if (StringUtil.isEmpty(str)) { return null; } return new BigDecimal(str); } /** * 功能:将字符串抓换为double,如果失败返回默认值。 * * @author Elfy * @date 2013年03月21日 * @param str * 字符串 * @param defaultValue * 失败时返回的默认值 * @return double */ public static double toDouble(String str, double defaultValue) { if (str == null) { return defaultValue; } try { return Double.parseDouble(str); } catch (NumberFormatException nfe) { return defaultValue; } } /** * 功能:将字符串抓换为float,如果失败返回默认值。 * * @author Elfy * @date 2013年03月21日 * @param str * 字符串 * @param defaultValue * 失败时返回的默认值 * @return float */ public static float toFloat(String str, float defaultValue) { if (str == null) { return defaultValue; } try { return Float.parseFloat(str); } catch (NumberFormatException nfe) { return defaultValue; } } /** * 功能:将字符串抓换为long,如果失败返回默认值。 * * @author Elfy * @date 2013年03月21日 * @param str * 字符串 * @param defaultValue * 失败时返回的默认值 * @return long */ public static long toLong(String str, long defaultValue) { if (str == null) { return defaultValue; } try { return Long.parseLong(str); } catch (NumberFormatException nfe) { return defaultValue; } } /** * 功能:将字符串抓换为int,如果失败返回默认值。 * * @author Elfy * @date 2013年03月21日 * @param str * 字符串 * @param defaultValue * 失败时返回的默认值 * @return int */ public static int toInt(String str, int defaultValue) { if (str == null) { return defaultValue; } try { return Integer.parseInt(str); } catch (NumberFormatException nfe) { return defaultValue; } } /** * <p> * 得到两个 <code>double</code>值中最大的一个. * </p> * * @param a * 值 1 * @param b * 值 2 * @return 最大的值 * @author Elfy * @date 2013年03月21日 */ public static float getMax(float a, float b) { if (Float.isNaN(a)) { return b; } else if (Float.isNaN(b)) { return a; } else { return Math.max(a, b); } } /** * <p> * 得到数组中最大的一个. * </p> * * @param array * 数组不能为null,也不能为空。 * @return 得到数组中最大的一个. * @throws IllegalArgumentException * 如果 <code>数组</code> 是 <code>null</code> * @throws IllegalArgumentException * 如果 <code>数组</code>是空 * @author Elfy * @date 2013年03月21日 */ public static float getMax(float[] array) { // Validates input if (array == null) { throw new IllegalArgumentException("The Array must not be null"); } else if (array.length == 0) { throw new IllegalArgumentException("Array cannot be empty."); } // Finds and returns max float max = array[0]; for (int j = 1; j < array.length; j++) { max = getMax(array[j], max); } return max; } /** * <p> * 得到数组中最大的一个. * </p> * * @param array * 数组不能为null,也不能为空。 * @return 得到数组中最大的一个. * @throws IllegalArgumentException * 如果 <code>数组</code> 是 <code>null</code> * @throws IllegalArgumentException * 如果 <code>数组</code>是空 * @author Elfy * @date 2013年03月21日 */ public static double getMax(double[] array) { // Validates input if (array == null) { throw new IllegalArgumentException("The Array must not be null"); } else if (array.length == 0) { throw new IllegalArgumentException("Array cannot be empty."); } // Finds and returns max double max = array[0]; for (int j = 1; j < array.length; j++) { max = getMax(array[j], max); } return max; } /** * <p> * 得到两个 <code>double</code>值中最大的一个. * </p> * * @param a * 值 1 * @param b * 值 2 * @return 最大的值 * @author Elfy * @date 2013年03月21日 * */ public static double getMax(double a, double b) { if (Double.isNaN(a)) { return b; } else if (Double.isNaN(b)) { return a; } else { return Math.max(a, b); } } /** * <p> * 得到两个float中最小的一个。 * </p> * * @param a * 值 1 * @param b * 值 2 * @return double值最小的 * @author Elfy * @date 2013年03月21日 */ public static float getMin(float a, float b) { if (Float.isNaN(a)) { return b; } else if (Float.isNaN(b)) { return a; } else { return Math.min(a, b); } } /** * <p> * 返回数组中最小的数值。 * </p> * * @param array * 数组不能为null,也不能为空。 * @return 数组里面最小的float * @throws IllegalArgumentException * 如果<code>数组</code>是<code>null</code> * @throws IllegalArgumentException * 如果<code>数组</code>是空 * @author Elfy * @date 2013年03月21日 */ public static float getMin(float[] array) { // Validates input if (array == null) { throw new IllegalArgumentException("数组不能为null。"); } else if (array.length == 0) { throw new IllegalArgumentException("数组不能为空。"); } // Finds and returns min float min = array[0]; for (int i = 1; i < array.length; i++) { min = getMin(array[i], min); } return min; } /** * <p> * 返回数组中最小的double。 * </p> * * @param array * 数组不能为null,也不能为空。 * @return 数组里面最小的double * @throws IllegalArgumentException * 如果<code>数组</code>是<code>null</code> * @throws IllegalArgumentException * 如果<code>数组</code>是空 * @author Elfy * @date 2013年03月21日 */ public static double getMin(double[] array) { // Validates input if (array == null) { throw new IllegalArgumentException("数组不能为null。"); } else if (array.length == 0) { throw new IllegalArgumentException("数组不能为空。"); } // Finds and returns min double min = array[0]; for (int i = 1; i < array.length; i++) { min = getMin(array[i], min); } return min; } /** * <p> * 得到两个double中最小的一个。 * </p> * * @param a * 值 1 * @param b * 值 2 * @return double值最小的 * @author Elfy * @date 2013年03月21日 */ public static double getMin(double a, double b) { if (Double.isNaN(a)) { return b; } else if (Double.isNaN(b)) { return a; } else { return Math.min(a, b); } } /** * 返回两个double的商 first除以second。 * * @param first * 第一个double * @param second * 第二个double * @return double * @author Elfy * @date 2013年03月21日 */ public static double divideDouble(double first, double second) { BigDecimal b1 = new BigDecimal(first); BigDecimal b2 = new BigDecimal(second); return b1.divide(b2).doubleValue(); } /** * 返回两个double的乘积 first*second。 * * @param first * 第一个double * @param second * 第二个double * @return double * @author Elfy * @date 2013年03月21日 */ public static double multiplyDouble(double first, double second) { BigDecimal b1 = new BigDecimal(first); BigDecimal b2 = new BigDecimal(second); return b1.multiply(b2).doubleValue(); } /** * 返回两个double的差值 first-second。 * * @param first * 第一个double * @param second * 第二个double * @return double * @author Elfy * @date 2013年03月21日 */ public static double subtractDouble(double first, double second) { BigDecimal b1 = new BigDecimal(first); BigDecimal b2 = new BigDecimal(second); return b1.subtract(b2).doubleValue(); } /** * 返回两个double的和值 first+second。 * * @param first * 第一个double * @param second * 第二个double * @return double * @author Elfy * @date 2013年03月21日 */ public static double sumDouble(double first, double second) { BigDecimal b1 = new BigDecimal(first); BigDecimal b2 = new BigDecimal(second); return b1.add(b2).doubleValue(); } /** * 格式化double指定位数小数。例如将11.123格式化为11.1。 * * @param value * 原double数字。 * @param decimals * 小数位数。 * @return 格式化后的double,注意为硬格式化不存在四舍五入。 * @author Elfy * @date 2013年03月21日 */ public static String formatDouble(double value, int decimals) { String doubleStr = "" + value; int index = doubleStr.indexOf(".") != -1 ? doubleStr.indexOf(".") : doubleStr.indexOf(","); // Decimal point can not be found... if (index == -1) return doubleStr; // Truncate all decimals if (decimals == 0) { return doubleStr.substring(0, index); } int len = index + decimals + 1; if (len >= doubleStr.length()) len = doubleStr.length(); double d = Double.parseDouble(doubleStr.substring(0, len)); return String.valueOf(d); } /** * 生成一个指定位数的随机数,并将其转换为字符串作为函数的返回值。 * * @param numberLength * 随机数的位数。 * @return String 注意随机数可能以0开头。 * @author Elfy * @date 2013年03月21日 */ public static String randomNumber(int numberLength) { // 记录生成的每一位随机数 StringBuffer sb = new StringBuffer(); for (int i = 0; i < numberLength; i++) { // 每次生成一位,随机生成一个0-10之间的随机数,不含10。 Double ranDouble = Math.floor(Math.random() * 10); sb.append(ranDouble.intValue()); } return sb.toString(); } /** * 功能:生成一个在最大数和最小数之间的随机数。会出现最小数,但不会出现最大数。 * * @author Elfy * @date 2013年03月21日 * @param minNum * 最小数 * @param maxNum * 最大数 * @return int */ public static int randomNumber(int minNum, int maxNum) { if (maxNum <= minNum) { throw new RuntimeException("maxNum必须大于minNum!"); } // 计算出来差值 int subtract = maxNum - minNum; Double ranDouble = Math.floor(Math.random() * subtract); return ranDouble.intValue() + minNum; } /** * 功能:生成一个在最大数和最小数之间的随机数。会出现最小数,但不会出现最大数。<br/> * 但不随机notin数组中指定的数字, 如果可随机的范围较小,可能会一直随机不到,或者随机的很慢。 * * @author Elfy * @date 2013年03月21日 * @param minNum * 最小数 * @param maxNum * 最大数 * @param notin * 不随机数组这些数字 * @return int */ public static int randomNumber(int minNum, int maxNum, Integer[] notin) { if (notin.length >= (maxNum - minNum)) { throw new RuntimeException("notin数组的元素已经把可以随机的都排除了,无法得到随机数!"); } while (true) { int num = randomNumber(minNum, maxNum); if (!CollectionUtil.arrayContain(notin, num)) { return num; } } } }
发表评论
-
spring MVC原理 TG【2013-04-08】
2017-04-28 11:28 653概述--Spring MVC工作流程图 图一 ... -
JAVA基础篇【工具类】加密解密(一)DES加密和解密 TG【20130331整理】
2016-11-28 10:24 0import java.io.UnsupportedEn ... -
JAVA基础篇【工具类】CollectionUtil 集合操作工具类 TG【20130318整理】
2016-11-28 10:13 591集合操作工具类 import java.util.Colle ... -
JAVA基础篇【工具类】ZipUtil zip工具类 TG【20130318整理】
2016-11-28 10:03 797import java.io.BufferedInputS ... -
JAVA基础篇【工具类】 SpellHelper 汉字转拼音 TG【20130318整理】
2016-11-28 10:00 469SpellHelper 该类是将汉字转化为拼音的工具类 ... -
JAVA基础篇【工具类】FtpUtil TG【20130317整理】
2016-11-28 09:51 268Ftp工具 在我们编程的时候,我们经常会遇到链接ftp存文件和 ... -
推荐CSS工具资源TG【20110309整理】
2016-11-14 17:27 369456 Berea Street http://www.45 ... -
JAVA基础篇【工具类】StringUtilsTG【20130316整理】
2016-11-14 17:29 342JAVA基础篇【工具类】StringUtilsTG【20110 ... -
浅谈JavaScript&TypeeScriptTG【20130305整理】
2016-11-14 17:02 4411.JavaScript 在1995年时 ...
相关推荐
主要为大家详细介绍了Java数学工具类MathUtil的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java计算工具类,类型转换,用于加减乘除。
包含各种工具类文件如ChangePinYin.java、CollectionUtil.java、DateUtil.java、DBConnectionUtil.java、FileUtil.java、FtpUtil.java、HttpClientUtil.java、MathUtil.java、MD5Util.java、StringUtil.java、...
工作中经常使用的Java工具类分享,做Java开发用得上。
有用的数学函数的集合。 源代码中的文档。 安装 只需安装到您的项目中: sampctl package install ScavengeSurvive/mathutil 包括在您的代码中并开始使用该库: #include < mathutil> 测验 要测试,只需运行该...
js基础工具类集合,集成方便后期调用,通过webpack编译,支持多种平台,如es6、node、浏览器引入等 使用 es6方法 安装 npm install xms-basic-util 导入 import xmsBasicUtil from 'xms-basic-util' //日期转换工具...
用于检查当前运行时环境信息fmtutil格式的数据util函数fsutil文件系统util函数jsonutil JSON util函数maputil map util函数mathutil数学util函数netutil网络实用程序功能strutil字符串util函数testutil测试帮助工具...
MathUtil
最近公司要求做个九宫格解锁,本人用的是小米手机...MathUtil.Java /** * @author SoBan * @create 2016/12/5 15:52. */ public class MathUtil { public static double distance(double x1, double y1, double x2, do
// mathutil_cuda_kernel.cu // 头文件,最后一个是cuda特有的 #include #include #include <math.h> #include #include mathutil_cuda_kernel.h // 获取GPU线程通道信息 dim3 cuda_gridsize(int n) { int...
但是,您可以在mathutil.py中mathutil.py我尚未测试的 Liang-Barsky 算法的快速草稿。 这是宾夕法尼亚大学 CIS 192 最终项目的延续。 这个项目的主要目的是教育。 这可能不适用于生产用途,但非常欢迎您将其用于该...
game.ReplicatedStorage.MathUtil local Math = {} function Math.round(numberToRound) return math.floor(numberToRound + 0.5) end return Math 您可以按照以下方式创建测试模块: game.ReplicatedStorage....