All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.smartboot.http.common.utils.NumberUtils Maven / Gradle / Ivy

There is a newer version: 2.5.1
Show newest version
/*******************************************************************************
 * Copyright (c) 2017-2020, org.smartboot. All rights reserved.
 * project name: smart-http
 * file name: NumberUtils.java
 * Date: 2020-01-01
 * Author: sandao ([email protected])
 ******************************************************************************/
package org.smartboot.http.common.utils;

/**
 * 

Provides extra functionality for Java Number classes.

* * @version $Id: NumberUtils.java 1507169 2013-07-26 01:03:52Z sebb $ * @since 2.0 */ public class NumberUtils { /** *

NumberUtils instances should NOT be constructed in standard programming. * Instead, the class should be used as NumberUtils.toInt("6");.

* *

This constructor is public to permit tools that require a JavaBean instance * to operate.

*/ public NumberUtils() { super(); } //----------------------------------------------------------------------- /** *

Convert a String to an int, returning a * default value if the conversion fails.

* *

If the string is null, the default value is returned.

* *
     *   NumberUtils.toInt(null, 1) = 1
     *   NumberUtils.toInt("", 1)   = 1
     *   NumberUtils.toInt("1", 0)  = 1
     * 
* * @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(final String str, final int defaultValue) { if (str == null) { return defaultValue; } try { return Integer.parseInt(str); } catch (final NumberFormatException nfe) { return defaultValue; } } public static long toLong(final String str, final long defaultValue) { if (str == null) { return defaultValue; } try { return Long.parseLong(str); } catch (final NumberFormatException nfe) { return defaultValue; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy