一、前言
基于apache开源包commons-pack中org.apache.commons.lang.NumberUtils数字类型工具类,对字符串转整型stringToInt、字符串转换浮点型createFloat、字符串转长整型createLong、字符串转大整型BigInteger、字符串转大数据整型BigDecimal、数据最小minimum、最大maximum、比较compare、是否为数字类型isNumber等等。
二、源码说明
package org.apache.commons.lang;@b@@b@import java.math.BigDecimal;@b@import java.math.BigInteger;@b@@b@/**@b@ * @deprecated@b@ */@b@public final class NumberUtils@b@{@b@ public static int stringToInt(String str)@b@ {@b@ return stringToInt(str, 0);@b@ }@b@@b@ public static int stringToInt(String str, int defaultValue)@b@ {@b@ try@b@ {@b@ return Integer.parseInt(str); } catch (NumberFormatException nfe) {@b@ }@b@ return defaultValue;@b@ }@b@@b@ public static Number createNumber(String val)@b@ throws NumberFormatException@b@ {@b@ String mant;@b@ String dec;@b@ String exp;@b@ if (val == null)@b@ return null;@b@@b@ if (val.length() == 0)@b@ throw new NumberFormatException("\"\" is not a valid number.");@b@@b@ if (val.startsWith("--"))@b@ {@b@ return null;@b@ }@b@ if ((val.startsWith("0x")) || (val.startsWith("-0x")))@b@ return createInteger(val);@b@@b@ char lastChar = val.charAt(val.length() - 1);@b@@b@ int decPos = val.indexOf(46);@b@ int expPos = val.indexOf(101) + val.indexOf(69) + 1;@b@@b@ if (decPos > -1)@b@ {@b@ if (expPos > -1) {@b@ if (expPos < decPos)@b@ throw new NumberFormatException(val + " is not a valid number.");@b@@b@ dec = val.substring(decPos + 1, expPos);@b@ } else {@b@ dec = val.substring(decPos + 1);@b@ }@b@ mant = val.substring(0, decPos);@b@ } else {@b@ if (expPos > -1)@b@ mant = val.substring(0, expPos);@b@ else@b@ mant = val;@b@@b@ dec = null;@b@ }@b@ if (!(Character.isDigit(lastChar))) {@b@ if ((expPos > -1) && (expPos < val.length() - 1))@b@ exp = val.substring(expPos + 1, val.length() - 1);@b@ else {@b@ exp = null;@b@ }@b@@b@ String numeric = val.substring(0, val.length() - 1);@b@ boolean allZeros = (isAllZeros(mant)) && (isAllZeros(exp));@b@ switch (lastChar)@b@ {@b@ case 'L':@b@ case 'l':@b@ if ((dec == null) && (exp == null) && ((((numeric.charAt(0) == '-') && (isDigits(numeric.substring(1)))) || (isDigits(numeric)))))@b@ {@b@ try@b@ {@b@ return createLong(numeric);@b@ }@b@ catch (NumberFormatException nfe)@b@ {@b@ return createBigInteger(numeric);@b@ }@b@ }@b@ throw new NumberFormatException(val + " is not a valid number.");@b@ case 'F':@b@ case 'f':@b@ try {@b@ Float f = createFloat(numeric);@b@ if ((!(f.isInfinite())) && (((f.floatValue() != 0.0F) || (allZeros))))@b@ {@b@ return f;@b@ }@b@ }@b@ catch (NumberFormatException e)@b@ {@b@ }@b@ case 'D':@b@ case 'd':@b@ try@b@ {@b@ Double d = createDouble(numeric);@b@ if ((!(d.isInfinite())) && (((d.floatValue() != 0.0D) || (allZeros))))@b@ return d;@b@ }@b@ catch (NumberFormatException d) {@b@ }@b@ }@b@ try {@b@ return createBigDecimal(numeric);@b@ }@b@ catch (NumberFormatException d)@b@ {@b@ throw new NumberFormatException(val + " is not a valid number.");@b@ }@b@@b@ }@b@@b@ if ((expPos > -1) && (expPos < val.length() - 1))@b@ exp = val.substring(expPos + 1, val.length());@b@ else@b@ exp = null;@b@@b@ if ((dec == null) && (exp == null));@b@ try@b@ {@b@ return createInteger(val);@b@ }@b@ catch (NumberFormatException allZeros)@b@ {@b@ try {@b@ return createLong(val);@b@ }@b@ catch (NumberFormatException nfe)@b@ {@b@ return createBigInteger(val);@b@ }@b@@b@ boolean allZeros = (isAllZeros(mant)) && (isAllZeros(exp));@b@ try {@b@ Float f = createFloat(val);@b@ if ((!(f.isInfinite())) && (((f.floatValue() != 0.0F) || (allZeros))))@b@ return f;@b@ }@b@ catch (NumberFormatException nfe)@b@ {@b@ }@b@ try {@b@ Double d = createDouble(val);@b@ if ((!(d.isInfinite())) && (((d.doubleValue() != 0.0D) || (allZeros))))@b@ return d;@b@ }@b@ catch (NumberFormatException d)@b@ {@b@ }@b@ }@b@ return createBigDecimal(val);@b@ }@b@@b@ private static boolean isAllZeros(String s)@b@ {@b@ if (s == null)@b@ return true;@b@@b@ for (int i = s.length() - 1; i >= 0; --i)@b@ if (s.charAt(i) != '0')@b@ return false;@b@@b@@b@ return (s.length() > 0);@b@ }@b@@b@ public static Float createFloat(String val)@b@ {@b@ return Float.valueOf(val);@b@ }@b@@b@ public static Double createDouble(String val)@b@ {@b@ return Double.valueOf(val);@b@ }@b@@b@ public static Integer createInteger(String val)@b@ {@b@ return Integer.decode(val);@b@ }@b@@b@ public static Long createLong(String val)@b@ {@b@ return Long.valueOf(val);@b@ }@b@@b@ public static BigInteger createBigInteger(String val)@b@ {@b@ BigInteger bi = new BigInteger(val);@b@ return bi;@b@ }@b@@b@ public static BigDecimal createBigDecimal(String val)@b@ {@b@ BigDecimal bd = new BigDecimal(val);@b@ return bd;@b@ }@b@@b@ public static long minimum(long a, long b, long c)@b@ {@b@ if (b < a)@b@ a = b;@b@@b@ if (c < a)@b@ a = c;@b@@b@ return a;@b@ }@b@@b@ public static int minimum(int a, int b, int c)@b@ {@b@ if (b < a)@b@ a = b;@b@@b@ if (c < a)@b@ a = c;@b@@b@ return a;@b@ }@b@@b@ public static long maximum(long a, long b, long c)@b@ {@b@ if (b > a)@b@ a = b;@b@@b@ if (c > a)@b@ a = c;@b@@b@ return a;@b@ }@b@@b@ public static int maximum(int a, int b, int c)@b@ {@b@ if (b > a)@b@ a = b;@b@@b@ if (c > a)@b@ a = c;@b@@b@ return a;@b@ }@b@@b@ public static int compare(double lhs, double rhs)@b@ {@b@ if (lhs < rhs)@b@ return -1;@b@@b@ if (lhs > rhs) {@b@ return 1;@b@ }@b@@b@ long lhsBits = Double.doubleToLongBits(lhs);@b@ long rhsBits = Double.doubleToLongBits(rhs);@b@ if (lhsBits == rhsBits) {@b@ return 0;@b@ }@b@@b@ if (lhsBits < rhsBits)@b@ return -1;@b@@b@ return 1;@b@ }@b@@b@ public static int compare(float lhs, float rhs)@b@ {@b@ if (lhs < rhs)@b@ return -1;@b@@b@ if (lhs > rhs) {@b@ return 1;@b@ }@b@@b@ int lhsBits = Float.floatToIntBits(lhs);@b@ int rhsBits = Float.floatToIntBits(rhs);@b@ if (lhsBits == rhsBits) {@b@ return 0;@b@ }@b@@b@ if (lhsBits < rhsBits)@b@ return -1;@b@@b@ return 1;@b@ }@b@@b@ public static boolean isDigits(String str)@b@ {@b@ if ((str == null) || (str.length() == 0))@b@ return false;@b@@b@ for (int i = 0; i < str.length(); ++i)@b@ if (!(Character.isDigit(str.charAt(i))))@b@ return false;@b@@b@@b@ return true;@b@ }@b@@b@ public static boolean isNumber(String str)@b@ {@b@ if (StringUtils.isEmpty(str))@b@ return false;@b@@b@ char[] chars = str.toCharArray();@b@ int sz = chars.length;@b@ boolean hasExp = false;@b@ boolean hasDecPoint = false;@b@ boolean allowSigns = false;@b@ boolean foundDigit = false;@b@@b@ int start = (chars[0] == '-') ? 1 : 0;@b@ if ((sz > start + 1) && @b@ (chars[start] == '0') && (chars[(start + 1)] == 'x')) {@b@ i = start + 2;@b@ if (i == sz) {@b@ return false;@b@ }@b@@b@ for (; i < chars.length; ++i)@b@ if ((((chars[i] < '0') || (chars[i] > '9'))) && (((chars[i] < 'a') || (chars[i] > 'f'))) && (((chars[i] < 'A') || (chars[i] > 'F'))))@b@ {@b@ return false;@b@ }@b@@b@ return true;@b@ }@b@@b@ --sz;@b@@b@ int i = start;@b@@b@ while ((i < sz) || ((i < sz + 1) && (allowSigns) && (!(foundDigit)))) {@b@ if ((chars[i] >= '0') && (chars[i] <= '9')) {@b@ foundDigit = true;@b@ allowSigns = false;@b@ }@b@ else if (chars[i] == '.') {@b@ if ((hasDecPoint) || (hasExp))@b@ {@b@ return false;@b@ }@b@ hasDecPoint = true;@b@ } else if ((chars[i] == 'e') || (chars[i] == 'E'))@b@ {@b@ if (hasExp)@b@ {@b@ return false;@b@ }@b@ if (!(foundDigit))@b@ return false;@b@@b@ hasExp = true;@b@ allowSigns = true;@b@ } else if ((chars[i] == '+') || (chars[i] == '-')) {@b@ if (!(allowSigns))@b@ return false;@b@@b@ allowSigns = false;@b@ foundDigit = false;@b@ } else {@b@ return false;@b@ }@b@ ++i;@b@ }@b@ if (i < chars.length) {@b@ if ((chars[i] >= '0') && (chars[i] <= '9'))@b@ {@b@ return true;@b@ }@b@ if ((chars[i] == 'e') || (chars[i] == 'E'))@b@ {@b@ return false;@b@ }@b@ if ((!(allowSigns)) && (((chars[i] == 'd') || (chars[i] == 'D') || (chars[i] == 'f') || (chars[i] == 'F'))))@b@ {@b@ return foundDigit;@b@ }@b@ if ((chars[i] == 'l') || (chars[i] == 'L'))@b@ {@b@ return ((foundDigit) && (!(hasExp)));@b@ }@b@@b@ return false;@b@ }@b@@b@ return ((!(allowSigns)) && (foundDigit));@b@ }@b@}