NumberUtils

来源:互联网 发布:男灵剑士捏脸数据图 编辑:程序博客网 时间:2024/05/21 11:02
package com.ihandy.xgxsigndome.util;


public class NumberUtils {


/**
* <p>
* Convert a <code>String</code> to an <code>int</code>, returning
* <code>zero</code> if the conversion fails.
* </p>

* <p>
* If the string is <code>null</code>, <code>zero</code> is returned.
* </p>

* <pre>
*   NumberUtils.toInt(null) = 0
*   NumberUtils.toInt("")   = 0
*   NumberUtils.toInt("1")  = 1
* </pre>

* @param str
*            the string to convert, may be null
* @return the int represented by the string, or <code>zero</code> if
*         conversion fails
* @since 2.1
*/
public static int toInt(String str) {
return toInt(str, 0);
}


/**
* <p>
* Convert a <code>String</code> to an <code>int</code>, returning a default
* value if the conversion fails.
* </p>

* <p>
* If the string is <code>null</code>, the default value is returned.
* </p>

* <pre>
*   NumberUtils.toInt(null, 1) = 1
*   NumberUtils.toInt("", 1)   = 1
*   NumberUtils.toInt("1", 0)  = 1
* </pre>

* @param str
*            the string to convert, may be null
* @param defaultValue
*            the default value
* @return the int represented by the string, or the default if conversion
*         fails
* @since 2.1
*/
public static int toInt(String str, int defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Integer.parseInt(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}


/**
* <p>
* Convert a <code>String</code> to a <code>double</code>, returning
* <code>0.0d</code> if the conversion fails.
* </p>

* <p>
* If the string <code>str</code> is <code>null</code>, <code>0.0d</code> is
* returned.
* </p>

* <pre>
*   NumberUtils.toDouble(null)   = 0.0d
*   NumberUtils.toDouble("")     = 0.0d
*   NumberUtils.toDouble("1.5")  = 1.5d
* </pre>

* @param str
*            the string to convert, may be <code>null</code>
* @return the double represented by the string, or <code>0.0d</code> if
*         conversion fails
* @since 2.1
*/
public static double toDouble(String str) {
return toDouble(str, 0.0d);
}


/**
* <p>
* Convert a <code>String</code> to a <code>double</code>, returning a
* default value if the conversion fails.
* </p>

* <p>
* If the string <code>str</code> is <code>null</code>, the default value is
* returned.
* </p>

* <pre>
*   NumberUtils.toDouble(null, 1.1d)   = 1.1d
*   NumberUtils.toDouble("", 1.1d)     = 1.1d
*   NumberUtils.toDouble("1.5", 0.0d)  = 1.5d
* </pre>

* @param str
*            the string to convert, may be <code>null</code>
* @param defaultValue
*            the default value
* @return the double represented by the string, or defaultValue if
*         conversion fails
* @since 2.1
*/
public static double toDouble(String str, double defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}


/**
* <p>
* Checks whether the String a valid Java number.
* </p>

* <p>
* Valid numbers include hexadecimal marked with the <code>0x</code>
* qualifier, scientific notation and numbers marked with a type qualifier
* (e.g. 123L).
* </p>

* <p>
* <code>Null</code> and empty String will return <code>false</code>.
* </p>

* @param str
*            the <code>String</code> to check
* @return <code>true</code> if the string is a correctly formatted number
*/
public static boolean isNumber(String str) {
if (StringUtils.isEmpty(str)) {
return false;
}
char[] chars = str.toCharArray();
int sz = chars.length;
boolean hasExp = false;
boolean hasDecPoint = false;
boolean allowSigns = false;
boolean foundDigit = false;
// deal with any possible sign up front
int start = (chars[0] == '-') ? 1 : 0;
if (sz > start + 1) {
if (chars[start] == '0' && chars[start + 1] == 'x') {
int i = start + 2;
if (i == sz) {
return false; // str == "0x"
}
// checking hex (it can't be anything else)
for (; i < chars.length; i++) {
if ((chars[i] < '0' || chars[i] > '9')
&& (chars[i] < 'a' || chars[i] > 'f')
&& (chars[i] < 'A' || chars[i] > 'F')) {
return false;
}
}
return true;
}
}
sz--; // don't want to loop to the last char, check it afterwords
// for type qualifiers
int i = start;
// loop to the next to last char or to the last char if we need another
// digit to
// make a valid number (e.g. chars[0..5] = "1234E")
while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
if (chars[i] >= '0' && chars[i] <= '9') {
foundDigit = true;
allowSigns = false;


} else if (chars[i] == '.') {
if (hasDecPoint || hasExp) {
// two decimal points or dec in exponent
return false;
}
hasDecPoint = true;
} else if (chars[i] == 'e' || chars[i] == 'E') {
// we've already taken care of hex.
if (hasExp) {
// two E's
return false;
}
if (!foundDigit) {
return false;
}
hasExp = true;
allowSigns = true;
} else if (chars[i] == '+' || chars[i] == '-') {
if (!allowSigns) {
return false;
}
allowSigns = false;
foundDigit = false; // we need a digit after the E
} else {
return false;
}
i++;
}
if (i < chars.length) {
if (chars[i] >= '0' && chars[i] <= '9') {
// no type qualifier, OK
return true;
}
if (chars[i] == 'e' || chars[i] == 'E') {
// can't have an E at the last byte
return false;
}
if (!allowSigns
&& (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
return foundDigit;
}
if (chars[i] == 'l' || chars[i] == 'L') {
// not allowing L with an exponent
return foundDigit && !hasExp;
}
// last character is illegal
return false;
}
// allowSigns is true iff the val ends in 'E'
// found digit it to make sure weird stuff like '.' and '1E-' doesn't
// pass
return !allowSigns && foundDigit;
}



/**
     * <p>Convert a <code>String</code> to a <code>long</code>, returning
     * <code>zero</code> if the conversion fails.</p>
     *
     * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
     *
     * <pre>
     *   NumberUtils.toLong(null) = 0L
     *   NumberUtils.toLong("")   = 0L
     *   NumberUtils.toLong("1")  = 1L
     * </pre>
     *
     * @param str  the string to convert, may be null
     * @return the long represented by the string, or <code>0</code> if
     *  conversion fails
     * @since 2.1
     */
    public static long toLong(String str) {
        return toLong(str, 0L);
    }
    
    /**
     * <p>Convert a <code>String</code> to a <code>long</code>, returning a
     * default value if the conversion fails.</p>
     *
     * <p>If the string is <code>null</code>, the default value is returned.</p>
     *
     * <pre>
     *   NumberUtils.toLong(null, 1L) = 1L
     *   NumberUtils.toLong("", 1L)   = 1L
     *   NumberUtils.toLong("1", 0L)  = 1L
     * </pre>
     *
     * @param str  the string to convert, may be null
     * @param defaultValue  the default value
     * @return the long represented by the string, or the default if conversion fails
     * @since 2.1
     */
    public static long toLong(String str, long defaultValue) {
        if (str == null) {
            return defaultValue;
        }
        try {
            return Long.parseLong(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }
}