请考察下面的代码:

 Long.parseLong("0", 10)        // returns 0L
 Long.parseLong("473", 10)      // returns 473L
 Long.parseLong("-0", 10)       // returns 0L
 Long.parseLong("-FF", 16)      // returns -255L
 Long.parseLong("1100110", 2)   // returns 102L
 Long.parseLong("99", 8)        // throws a NumberFormatException
 Long.parseLong("Hazelnut", 10) // throws a NumberFormatException
 Long.parseLong("Hazelnut", 36) // returns 1356099454469L

上面的代码是转换为 Long 的。

转换为 Float 也是一样的。

实际上,我们可能会用到下面的代码来转换。

NumberUtils.toLong(“473”);

NumberUtils 这个工具类是在

package org.apache.commons.lang3.math

包中的java字符串转化为数字,同时主要也是为了避免出现 null 对象的转换异常。

java字符和字符串转化_asp判断字符是否为数字_java字符串转化为数字

根据官方的说法为:如果输入的字符串为 null 或者 0 的话java字符串转化为数字,将会有下面的返回和输出。


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

限时特惠:本站每日持续更新海量设计资源,一年会员只需29.9元,全站资源免费下载
站长微信:ziyuanshu688