Java Number Parse toNumber(Object object, Number defaultValue)

Here you can find the source of toNumber(Object object, Number defaultValue)

Description

Extract the value represented by the given object (Number or String) - trialing '.0*' will be removed.

License

Open Source License

Parameter

Parameter Description
object Integer or Double, fall back to defaultValue.
defaultValue default value to be returned if object is null or NumberFormatException .

Return

the value extracted or default value if failed to extract the value.

Declaration

public static Number toNumber(Object object, Number defaultValue) 

Method Source Code

//package com.java2s;
/**********************************************************************************************
 *
 * Asprise Scanning and Imaging API/*from   w ww.j a v a2s  . c om*/
 * Copyright (C) 1998-2016. Asprise Inc. <asprise.com>
 *
 * This file is licensed under the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation.
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * You should have received a copy of the GNU Affero General Public License.  If not, please
 * visit <http://www.gnu.org/licenses/agpl-3.0.html>.
 *
 **********************************************************************************************/

public class Main {
    /**
     * Extract the value represented by the given object (Number or String) - trialing '.0*' will be removed.
     * @param object Integer or Double, fall back to defaultValue.
     * @param defaultValue default value to be returned if object is null or {@linkplain NumberFormatException}.
     * @return the value extracted or default value if failed to extract the value.
     */
    public static Number toNumber(Object object, Number defaultValue) {
        if (object == null) {
            return defaultValue;
        }
        if (object instanceof Number) {
            return (Number) object;
        }

        String s = object.toString();
        s = s.replaceAll("\\.0*$", ""); // remove trialing ".0"
        try {
            if (s.contains(".")) {
                return Double.parseDouble(s);
            } else {
                return Integer.parseInt(s);
            }
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    /**
     * Return null if object is null otherwise object.toString().
     * @param object
     * @return
     */
    public static String toString(Object object) {
        return object == null ? null : object.toString();
    }
}

Related

  1. isNumeric(String value, Locale locale)
  2. isParsable(Object parser, String str)
  3. toNumber(char letter)
  4. toNumber(CharSequence jsonText)
  5. toNumber(final Object obj)
  6. toNumber(Object object, Number defaultValue)
  7. toNumber(String hash)
  8. toNumber(String num)
  9. toNumber(String s)