java中Stringjava字符串转化成数字为数字

博客分类:
java数字和字符串之间的转换工具
package com.test.
* 数字工具类
public class NumberUtil {
* 数字转换为字符串
* @param num 数字
* @return 字符串,如果 num 为空, 返回空字符串
public static String num2Str(Object num) {
String str =
if (num == null) {
str = String.valueOf(num);
* 字符串转换为Integer
* @param str 字符串
* @return Integer, str为null时返回0
public static Integer getInteger(Object obj) {
return getInteger(obj, 0);
* 字符串转换为Integer
* @param str 字符串
* @param def 默认值
* @return Integer, 字符串为null时返回def
public static Integer getInteger(Object obj, int def) {
String str = obj == null ? "" : obj.toString();
Integer i =
if (str.trim().length() == 0) {
i = new Integer(def);
i = Integer.valueOf(str);
catch (Exception e) {
return i == null ? new Integer(def) :
* 字符串转换为Long
* @param str 字符串
* @return Long, str为null时返回0
public static Long getLong(Object obj) {
return getLong(obj, 0);
* 字符串转换为Long
* @param str 字符串
* @param def 默认值
* @return Long, 字符串为null时返回def
public static Long getLong(Object obj, long def) {
String str = obj == null ? "" : obj.toString();
if (str.trim().length() == 0) {
l = new Long(def);
l = Long.valueOf(str);
catch (Exception e) {
return l == null ? new Long(def) :
* 字符串转换为Integer
* @param str 字符串
* @return Integer, str为null时返回0
public static int getIntegerValue(Object obj) {
return getIntegerValue(obj, 0);
* 字符串转换为Integer
* @param str 字符串
* @param def 默认值
* @return Integer, 字符串为null时返回def
public static int getIntegerValue(Object obj, int def) {
return getInteger(obj, def).intValue();
* 字符串转换为Long
* @param str 字符串
* @return Long, str为null时返回0
public static long getLongValue(Object obj) {
return getLongValue(obj, 0);
* 字符串转换为Long
* @param str 字符串
* @param def 默认值
* @return Long, 字符串为null时返回def
public static long getLongValue(Object obj, long def) {
return getLong(obj, def).longValue();
浏览: 39651 次
来自: 南京
你这个根本就不对,我遍历了5遍,每次的到的count结果都不一 ...
spiniper 写道你这三段代码根本就没可比性。why?
你这三段代码根本就没可比性。
不错哦 给个赞
zx_code 写道郑州蘑菇云 写道为什么必须要加入呢?直接使 ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'java、scala判断字符串是否能转换为数字
09:52:06&&&作者:MangoCool&&&来源:MangoCool
在处理数据之前,需要判断能否转为这种类型,一直没找到相应的API,于是自己实现了一个简单粗暴的方法,记录下来,以供交流。
java版本:
package com.mangocool.
import java.text.SimpleDateF
* Created by MANGOCOOL on .
public class VerifyTest {
public static boolean verify(String str, String type)
if("double".equals(type))
Double.valueOf(str);
} else if("int".equals(type))
Integer.valueOf(str);
else if("date".equals(type))
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.parse(str);
} catch (Exception e) {
//如果抛出异常,返回False
public static void main(String[] args) {
String str = "88.8";
System.out.println(verify(str, "double"));
System.out.println(verify(str, "int"));
System.out.println(verify(str, "date"));
scala版本:
package com.mangocool.verify
import scala.util.{Success, Try}
* Created by MANGOCOOL on .
object VerifyTest2 {
def verify(str: String, dtype: String):Boolean = {
var c:Try[Any] = null
if("double".equals(dtype)) {
c = scala.util.Try(str.toDouble)
} else if("int".equals(dtype)) {
c = scala.util.Try(str.toInt)
val result = c match {
case Success(_) =&
def main (args: Array[String] ): Unit = {
val str = "88.8"
println(verify(str, "double"))
println(verify(str, "int"))
println(verify(str, "date"))
记录至此,仅供参考,欢迎交流!
联系电话:151****5656
工作邮箱:
芒果酷(mangocool.com) All rights reserved. 湘ICP备号
免责声明:本网站部分文章转载其他媒体,意在为公众提供免费服务。如有信息侵犯了您的权益,可与本网站联系,本网站将尽快予以撤除。<span style="color: # //方法一:用JAVA自带的函数
<span style="color: # public static boolean isNumeric(String str){
<span style="color: #
for (int i = str.length();--i&=0;){
<span style="color: #
if (!Character.isDigit(str.charAt(i))){
<span style="color: #
return false;
<span style="color: #
<span style="color: #
<span style="color: #
return true;
<span style="color: # }
/*方法二:推荐,速度最快
* 判断是否为整数
* @param str 传入的字符串
* @return 是整数返回true,否则返回false
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
//方法三:
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
//方法四:
public final static boolean isNumeric(String s) {
if (s != null && !"".equals(s.trim()))
return s.matches("^[0-9]*$");
return false;
//方法五:用ascii码
public static boolean isNumeric(String str){
for(int i=str.length();--i&=0;){
int chr=str.charAt(i);
if(chr&48 || chr&57)
return false;
return true;
转自:http://javapub.iteye.com/blog/666544
阅读(...) 评论()java中判断字符串是否为数字的三种方法
1用JAVA自带的函数
public static boolean isNumericFirst(String str){
for (int i = str.length();--i&=0;){
if (!Character.isDigit(str.charAt(i))){
//2用正则表达式
public static boolean isNumericSecond(String str){
Pattern pattern = Pattern.compile(&[0-9]*&);
return pattern.matcher(str).matches();
//3用ascii码
public static boolean isNumericThrid(String str){
for(int i=str.length();--i&=0;){
int chr=str.charAt(i);
if(chr&48 || chr&57)一、设计思路
在命令行中输入的几个参数都是字符串,先将其转化为数字,再进行运算,在输出时再将其转化为字符串输出。
二、程序流程图
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
三、源代码
//编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
import&java.util.S
&public&class&Add
public&static&void&main(String[]&args)&
//&TODO&Auto-generated&method&stub&
Scanner&scan&=&new&Scanner(System.in);&&//&创建Scanner类对象
System.out.println("请输入几个数字求和:");
n&=&scan.nextInt();&&//输入要求和的数的数量
double[]&aDoubletA
aDoubletArray&=&new&double[n];&&//定义n个双精度参数
System.out.print("请输入"&+n&);
System.out.println("个数字:");
for(int&i&=&0;&i&aDoubletArray.&i++)
aDoubletArray[i]&=&scan.nextInt();&&&//输入n个求和的数
}&&&&&&//把输入的字符串转化成浮点型
double&result&=&0;
for(int&i&=&0;&i&aDoubletArray.&i++)
result&+=&aDoubletArray[i];
System.out.println("相加结果为:"&+&result);//计算并输出结果
四、结果截图
&&&&&&&&&&&&&&&
阅读(...) 评论()}

我要回帖

更多关于 java整形转化为字符串 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信