Java Long Number Create toLong(String string)

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

Description

Convert String to an long.

License

Open Source License

Parameter

Parameter Description
string A String containing an integer.

Return

an int

Declaration

public static long toLong(String string) 

Method Source Code

//package com.java2s;
//  are made available under the terms of the Eclipse Public License v1.0

public class Main {
    /**/* w w w .  ja v a  2 s . c om*/
     * Convert String to an long. Parses up to the first non-numeric character. If no number is found an IllegalArgumentException is thrown
     * 
     * @param string
     *            A String containing an integer.
     * @return an int
     */
    public static long toLong(String string) {
        long val = 0;
        boolean started = false;
        boolean minus = false;

        for (int i = 0; i < string.length(); i++) {
            char b = string.charAt(i);
            if (b <= ' ') {
                if (started)
                    break;
            } else if (b >= '0' && b <= '9') {
                val = val * 10L + (b - '0');
                started = true;
            } else if (b == '-' && !started) {
                minus = true;
            } else
                break;
        }

        if (started)
            return minus ? (-val) : val;
        throw new NumberFormatException(string);
    }
}

Related

  1. toLong(String str)
  2. toLong(String str)
  3. toLong(String str, long defaultValue)
  4. toLong(String str, long val)
  5. toLong(String string)
  6. toLong(String string)
  7. toLong(String string)
  8. toLong(String strVal)
  9. toLong(String time)