📄 long.java
字号:
buf[--charPos] = sign; } return charPos; } static void appendTo(long i, StringBuffer sb) { if (i == Long.MIN_VALUE) { sb.append("-9223372036854775808"); return; } char[] buf = (char[])(perThreadBuffer.get()); int charPos = getChars(i, buf); sb.append(buf, charPos, (20 - charPos)); return; } /** * Parses the string argument as a signed <code>long</code> in the * radix specified by the second argument. The characters in the * string must all be digits of the specified radix (as determined * by whether {@link java.lang.Character#digit(char, int)} returns * a nonnegative value), except that the first character may be an * ASCII minus sign <code>'-'</code> (<code>'\u002D'</code>) to * indicate a negative value. The resulting <code>long</code> * value is returned. * <p> * Note that neither the character <code>L</code> * (<code>'\u004C'</code>) nor <code>l</code> * (<code>'\u006C'</code>) is permitted to appear at the end * of the string as a type indicator, as would be permitted in * Java programming language source code - except that either * <code>L</code> or <code>l</code> may appear as a digit for a * radix greater than 22. * <p> * An exception of type <code>NumberFormatException</code> is * thrown if any of the following situations occurs: * <ul> * <li>The first argument is <code>null</code> or is a string of * length zero. * <li>The <code>radix</code> is either smaller than {@link * java.lang.Character#MIN_RADIX} or larger than {@link * java.lang.Character#MAX_RADIX}. * <li>Any character of the string is not a digit of the specified * radix, except that the first character may be a minus sign * <code>'-'</code> (<code>'\u002d'</code>) provided that the * string is longer than length 1. * <li>The value represented by the string is not a value of type * <code>long</code>. * </ul><p> * Examples: * <blockquote><pre> * parseLong("0", 10) returns 0L * parseLong("473", 10) returns 473L * parseLong("-0", 10) returns 0L * parseLong("-FF", 16) returns -255L * parseLong("1100110", 2) returns 102L * parseLong("99", 8) throws a NumberFormatException * parseLong("Hazelnut", 10) throws a NumberFormatException * parseLong("Hazelnut", 36) returns 1356099454469L * </pre></blockquote> * * @param s the <code>String</code> containing the * <code>long</code> representation to be parsed. * @param radix the radix to be used while parsing <code>s</code>. * @return the <code>long</code> represented by the string argument in * the specified radix. * @exception NumberFormatException if the string does not contain a * parsable <code>long</code>. */ public static long parseLong(String s, int radix) throws NumberFormatException { if (s == null) { throw new NumberFormatException("null"); } if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } long result = 0; boolean negative = false; int i = 0, max = s.length(); long limit; long multmin; int digit; if (max > 0) { if (s.charAt(0) == '-') { negative = true; limit = Long.MIN_VALUE; i++; } else { limit = -Long.MAX_VALUE; } multmin = limit / radix; if (i < max) { digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } else { result = -digit; } } while (i < max) { // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++),radix); if (digit < 0) { throw NumberFormatException.forInputString(s); } if (result < multmin) { throw NumberFormatException.forInputString(s); } result *= radix; if (result < limit + digit) { throw NumberFormatException.forInputString(s); } result -= digit; } } else { throw NumberFormatException.forInputString(s); } if (negative) { if (i > 1) { return result; } else { /* Only got "-" */ throw NumberFormatException.forInputString(s); } } else { return -result; } } /** * Parses the string argument as a signed decimal * <code>long</code>. The characters in the string must all be * decimal digits, except that the first character may be an ASCII * minus sign <code>'-'</code> (<code>\u002D'</code>) to * indicate a negative value. The resulting <code>long</code> * value is returned, exactly as if the argument and the radix * <code>10</code> were given as arguments to the {@link * #parseLong(java.lang.String, int)} method. * <p> * Note that neither the character <code>L</code> * (<code>'\u004C'</code>) nor <code>l</code> * (<code>'\u006C'</code>) is permitted to appear at the end * of the string as a type indicator, as would be permitted in * Java programming language source code. * * @param s a <code>String</code> containing the <code>long</code> * representation to be parsed * @return the <code>long</code> represented by the argument in * decimal. * @exception NumberFormatException if the string does not contain a * parsable <code>long</code>. */ public static long parseLong(String s) throws NumberFormatException { return parseLong(s, 10); } /** * Returns a <code>Long</code> object holding the value * extracted from the specified <code>String</code> when parsed * with the radix given by the second argument. The first * argument is interpreted as representing a signed * <code>long</code> in the radix specified by the second * argument, exactly as if the arguments were given to the {@link * #parseLong(java.lang.String, int)} method. The result is a * <code>Long</code> object that represents the <code>long</code> * value specified by the string. * <p> * In other words, this method returns a <code>Long</code> object equal * to the value of: * * <blockquote><code> * new Long(Long.parseLong(s, radix)) * </code></blockquote> * * @param s the string to be parsed * @param radix the radix to be used in interpreting <code>s</code> * @return a <code>Long</code> object holding the value * represented by the string argument in the specified * radix. * @exception NumberFormatException If the <code>String</code> does not * contain a parsable <code>long</code>. */ public static Long valueOf(String s, int radix) throws NumberFormatException { return new Long(parseLong(s, radix)); } /** * Returns a <code>Long</code> object holding the value * of the specified <code>String</code>. The argument is * interpreted as representing a signed decimal <code>long</code>, * exactly as if the argument were given to the {@link * #parseLong(java.lang.String)} method. The result is a * <code>Long</code> object that represents the integer value * specified by the string. * <p> * In other words, this method returns a <code>Long</code> object * equal to the value of: * * <blockquote><pre> * new Long(Long.parseLong(s)) * </pre></blockquote> * * @param s the string to be parsed. * @return a <code>Long</code> object holding the value * represented by the string argument. * @exception NumberFormatException If the string cannot be parsed * as a <code>long</code>. */ public static Long valueOf(String s) throws NumberFormatException { return new Long(parseLong(s, 10)); } /** * Decodes a <code>String</code> into a <code>Long</code>. * Accepts decimal, hexadecimal, and octal numbers given by the * following grammar: * * <blockquote> * <dl> * <dt><i>DecodableString:</i> * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i> * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i> * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i> * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i> * <p> * <dt><i>Sign:</i> * <dd><code>-</code> * </dl> * </blockquote> * * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a> * of the <a href="http://java.sun.com/docs/books/jls/html/">Java * Language Specification</a>. * <p> * The sequence of characters following an (optional) negative * sign and/or radix specifier ("<code>0x</code>", * "<code>0X</code>", "<code>#</code>", or * leading zero) is parsed as by the <code>Long.parseLong</code> * method with the indicated radix (10, 16, or 8). This sequence * of characters must represent a positive value or a {@link * NumberFormatException} will be thrown. The result is negated * if first character of the specified <code>String</code> is the * minus sign. No whitespace characters are permitted in the * <code>String</code>. * * @param nm the <code>String</code> to decode. * @return a <code>Long</code> object holding the <code>long</code> * value represented by <code>nm</code> * @exception NumberFormatException if the <code>String</code> does not * contain a parsable <code>long</code>. * @see java.lang.Long#parseLong(String, int) * @since 1.2 */ public static Long decode(String nm) throws NumberFormatException { int radix = 10; int index = 0; boolean negative = false; Long result; // Handle minus sign, if present if (nm.startsWith("-")) { negative = true; index++; } // Handle radix specifier, if present if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { index += 2; radix = 16; } else if (nm.startsWith("#", index)) { index ++; radix = 16; } else if (nm.startsWith("0", index) && nm.length() > 1 + index) { index ++; radix = 8; } if (nm.startsWith("-", index)) throw new NumberFormatException("Negative sign in wrong position"); try { result = Long.valueOf(nm.substring(index), radix); result = negative ? new Long((long)-result.longValue()) : result; } catch (NumberFormatException e) { // If number is Long.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? new String("-" + nm.substring(index)) : nm.substring(index); result = Long.valueOf(constant, radix); } return result; } /** * The value of the <code>Long</code>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -