Java Long Number Create toLong(String str)

Here you can find the source of toLong(String str)

Description

Convert a String to a long, returning zero if the conversion fails.

If the string is null, zero is returned.

 NumberUtility.toLong(null) = 0L NumberUtility.toLong("")   = 0L NumberUtility.toLong("1")  = 1L 

License

Open Source License

Parameter

Parameter Description
str the string to convert, may be null

Return

the long represented by the string, or 0 if conversion fails

Declaration

public static long toLong(String str) 

Method Source Code

//package com.java2s;
/*//  ww  w.  ja v  a2s  .c o m
 * Copyright (c) 2007-2016 AREasy Runtime
 *
 * This library, AREasy Runtime and API for BMC Remedy AR System, is free software ("Licensed Software");
 * you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either version 2.1 of the License,
 * or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * including but not limited to, the implied warranty of MERCHANTABILITY, NONINFRINGEMENT,
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
 */

public class Main {
    /**
     * <p>Convert a <code>String</code> to a <code>long</code>, returning
     * <code>zero</code> if the conversion fails.</p>
     * <p/>
     * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
     * <p/>
     * <pre>
     *   NumberUtility.toLong(null) = 0L
     *   NumberUtility.toLong("")   = 0L
     *   NumberUtility.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
     */
    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/>
     * <p>If the string is <code>null</code>, the default value is returned.</p>
     * <p/>
     * <pre>
     *   NumberUtility.toLong(null, 1L) = 1L
     *   NumberUtility.toLong("", 1L)   = 1L
     *   NumberUtility.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
     */
    public static long toLong(String str, long defaultValue) {
        if (str == null)
            return defaultValue;

        try {
            return Long.parseLong(str);
        } catch (NumberFormatException nfe) {
            return defaultValue;
        }
    }

    /**
     * Convert a double number into a long
     * @param num double value to be converted
     * @return converted long value
     */
    public static long toLong(double num) {
        return (new Double(num)).longValue();
    }

    /**
     * Convert a float number into a long
     * @param num float value to be converted
     * @return converted long value
     */
    public static long toLong(float num) {
        return (new Float(num)).longValue();
    }
}

Related

  1. toLong(String s)
  2. ToLong(String s)
  3. toLong(String s, long defValue)
  4. toLong(String src)
  5. toLong(String str)
  6. toLong(String str)
  7. toLong(String str)
  8. toLong(String str)
  9. toLong(String str)