commons-lang3之NumberUtils源码解析

源码版本

commons-lang3-3.1.jar

function

Provides extra functionality for Java Number classes.

源码介绍

字符串转化成int

/**
 * <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>
 * @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>
 * @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;
    }
}

字符串转化成long

public static long toLong(String str) {
    return toLong(str, 0L);
}

public static long toLong(String str, long defaultValue) {
    if (str == null) {
        return defaultValue;
    }
    try {
        return Long.parseLong(str);
    } catch (NumberFormatException nfe) {
        return defaultValue;
    }
}

类似的还有toFloat、toDouble、toByte、toShort

字符串转成Integer


public static Integer createInteger(String str) {
    if (str == null) {
        return null;
    }
    // decode() handles 0xAABD and 0777 (hex and octal) as well.
    return Integer.decode(str);
}

Min in array

    /**
     * <p>Returns the minimum value in an array.</p>
     * 
     */
public static long min(long[] 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 min
    long min = array[0];
    for (int i = 1; i < array.length; i++) {
        if (array[i] < min) {
            min = array[i];
        }
    }

    return min;
}

Max in array

/**
 * <p>Returns the maximum value in an array.</p>
 */
public static long max(long[] 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
    long max = array[0];
    for (int j = 1; j < array.length; j++) {
        if (array[j] > max) {
            max = array[j];
        }
    }

    return max;
}

Gets the maximum of three int

public static int max(int a, int b, int c) {
    if (b > a) {
        a = b;
    }
    if (c > a) {
        a = c;
    }
    return a;
}

Gets the minimum of three int

public static int min(int a, int b, int c) {
    if (b < a) {
        a = b;
    }
    if (c < a) {
        a = c;
    }
    return a;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值