将String类型的数字字符转换成int

java.lang.Integer.parseInt(String)

public static int parseInt(String s)
                    throws NumberFormatException
Parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ (‘\u002D‘) to indicate a negative value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseInt(java.lang.String, int) method.

Parameters:
s - a String containing the int representation to be parsed
Returns:
the integer value represented by the argument in decimal.
Throws:
NumberFormatException - if the string does not contain a parsable integer.
 

为什么不是

java.lang.Integer.valueOf(String)

看代码:

    /**
     * Returns an <code>Integer</code> object holding the
     * value of the specified <code>String</code>. The argument is
     * interpreted as representing a signed decimal integer, exactly
     * as if the argument were given to the {@link
     * #parseInt(java.lang.String)} method. The result is an
     * <code>Integer</code> object that represents the integer value
     * specified by the string.
     * <p>
     * In other words, this method returns an <code>Integer</code>
     * object equal to the value of:
     *
     * <blockquote><code>
     * new Integer(Integer.parseInt(s))
     * </code></blockquote>
     *
     * @param      s   the string to be parsed.
     * @return     an <code>Integer</code> object holding the value
     *             represented by the string argument.
     * @exception  NumberFormatException  if the string cannot be parsed
     *             as an integer.
     */
    public static Integer valueOf(String s) throws NumberFormatException
    {
    return new Integer(parseInt(s, 10));
    }
    /**
     * Parses the string argument as a signed integer 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 integer value is returned.
     * <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 radix 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>int</code>.
     * </ul><p>
     * Examples:
     * <blockquote><pre>
     * parseInt("0", 10) returns 0
     * parseInt("473", 10) returns 473
     * parseInt("-0", 10) returns 0
     * parseInt("-FF", 16) returns -255
     * parseInt("1100110", 2) returns 102
     * parseInt("2147483647", 10) returns 2147483647
     * parseInt("-2147483648", 10) returns -2147483648
     * parseInt("2147483648", 10) throws a NumberFormatException
     * parseInt("99", 8) throws a NumberFormatException
     * parseInt("Kona", 10) throws a NumberFormatException
     * parseInt("Kona", 27) returns 411787
     * </pre></blockquote>
     *
     * @param      s   the <code>String</code> containing the integer
     *             representation to be parsed
     * @param      radix   the radix to be used while parsing <code>s</code>.
     * @return     the integer represented by the string argument in the
     *             specified radix.
     * @exception  NumberFormatException if the <code>String</code>
     *            does not contain a parsable <code>int</code>.
     */
    public static int parseInt(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");
    }

    int result = 0;
    boolean negative = false;
    int i = 0, max = s.length();
    int limit;
    int multmin;
    int digit;

    if (max > 0) {
        if (s.charAt(0) == ‘-‘) {
        negative = true;
        limit = Integer.MIN_VALUE;
        i++;
        } else {
        limit = -Integer.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 integer. 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
     * integer value is returned, exactly as if the argument and the radix
     * 10 were given as arguments to the
     * {@link #parseInt(java.lang.String, int)} method.
     *
     * @param s       a <code>String</code> containing the <code>int</code>
     *             representation to be parsed
     * @return     the integer value represented by the argument in decimal.
     * @exception  NumberFormatException  if the string does not contain a
     *               parsable integer.
     */
    public static int parseInt(String s) throws NumberFormatException {
    return parseInt(s,10);
    }
时间: 2024-11-08 08:10:28

将String类型的数字字符转换成int的相关文章

关于springmvc怎么自动把前台string类型日期字段转换成date类型

关于springmvc怎么把前台string类型日期字段转换成date类型字段,小狼想了好久,阅读spring源代码,发现一个很好玩的注解@DateTimeFormat(pattern="yyyy-MM-dd") 对,就是他,小狼是这么使用的 @Controller public class Login { @RequestMapping("/login") public String login(String username,@DateTimeFormat(pa

Swift - 将String类型的数字转换成数字类型(支持十进制、十六进制)

1,十进制的字符串转成数字 Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = (tf1.text as NSString).intValue var f = (tf1.text as NSString).floatValue var d = (tf1.text as NSString).doubleValue 2,十六进制的字符串转成数字 (1)定义一个转换方法 1 2 3

Swift - 将String类型的数字转换成数字类型

Swift中,如果要把字符串转换成数字类型(比如整型,浮点型等).可以先转成NSString类型,让后再转. 1 2 3 4 //将文本框中的值转换成数字 var i = (tf1.text as NSString).intValue var f = (tf1.text as NSString).floatValue var d = (tf1.text as NSString).doubleValue

java中如何把一个String类型的变量转换成double型的?

Double.parseDouble(String类型变量) 例如定义String变量A为“10”,将String转化为Double变量. 我写出来了,你可以看一下,如下图: 拓展资料: int 转化成String: int i=10; String str=Integer.toString(i); double 转化成String: double d=10.0; String str=Double.toString(d); String 转化成double: String str="12345

浮点型float数据强制转换成int整型

问题:如下代码 想获取某两个Decimal类型数之间的商的大小,结果偶尔出错(请注意是 偶尔) Decima t1; Decima t2; int shang =Convert.ToInt32(t1 / t2) ; 解决方法:将Decimal类型数据强制转换成INT整型时  会有四舍五入的过程.如下,需要用Math.Truncate方法来取整数位.所以区商时必须用此方法取整 问题代码: Decima t1=1.2m; Convert.ToInt32(t1)  得到1 Decima t2=1.7m

数组冒泡排序,文件读取,数据库读取,string类型的int数组转换成int数组

排序方式(枚举) 1 public enum SortBy 2 { 3 Asc, 4 Desc 5 } 数组冒泡排序方法 1 public class SortEntity 2 { 3 public static int[] SortArray(int[] array,SortBy sortby) 4 { 5 int flag; 6 switch (sortby) 7 { 8 case SortBy.Asc: 9 for (int i = 0; i < array.Length - 1; i++

C++中将string类型变量转换成int型变量

需要的头文件:#include<sstream> 操作: string s1="124": int x; stringstream ss; ss<<s1; ss>>x; C++中将string类型变量转换成int型变量,布布扣,bubuko.com

MySQL类型转换 使用CAST将varchar转换成int类型排序

mysql为我们提供了两个类型转换函数:CAST和CONVERT,现成的东西我们怎能放过? 1 BINARY[(N)] 2 CHAR[(N)] 3 DATE 4 DATETIME 5 DECIMAL 6 SIGNED [INTEGER] 7 TIME 8 UNSIGNED [INTEGER] 例子: --使用CAST将varchar转换成int类型排序 select server_id from cardserver where game_id = 1 order by CAST(server_

CString转换成int CString类相应函数

CString 型转化成 int 型 把 CString 类型的数据转化成整数类型最简单的方法就是使用标准的字符串到整数转换例程. 虽然通常你怀疑使用_atoi()函数是一个好的选择,它也很少会是一个正确的选择.如果你准备使用 Unicode 字符,你应该用_ttoi(),它在 ANSI 编码系统中被编译成_atoi(),而在 Unicode 编码系统中编译成_wtoi().你也可以考虑使用_tcstoul()或者_tcstol(),它们都能把字符串转化成任意进制的长整数(如二进制.八进制.十进