java实现字符串和数字转换工具
本文实例为大家分享了java字符串和数字转换工具的具体代码,供大家参考,具体内容如下
packagecom.test.util;
/**
*数字工具类
*/
publicclassNumberUtil{
/**
*数字转换为字符串
*@paramnum数字
*@return字符串,如果num为空,返回空字符串
*/
publicstaticStringnum2Str(Objectnum){
Stringstr=null;
if(num==null){
str="";
}
else{
str=String.valueOf(num);
}
returnstr;
}
/**
*字符串转换为Integer
*@paramstr字符串
*@returnInteger,str为null时返回0
*/
publicstaticIntegergetInteger(Objectobj){
returngetInteger(obj,0);
}
/**
*字符串转换为Integer
*@paramstr字符串
*@paramdef默认值
*@returnInteger,字符串为null时返回def
*/
publicstaticIntegergetInteger(Objectobj,intdef){
Stringstr=obj==null?"":obj.toString();
Integeri=null;
if(str.trim().length()==0){
i=newInteger(def);
}
else{
try{
i=Integer.valueOf(str);
}
catch(Exceptione){
}
}
returni==null?newInteger(def):i;
}
/**
*字符串转换为Long
*@paramstr字符串
*@returnLong,str为null时返回0
*/
publicstaticLonggetLong(Objectobj){
returngetLong(obj,0);
}
/**
*字符串转换为Long
*@paramstr字符串
*@paramdef默认值
*@returnLong,字符串为null时返回def
*/
publicstaticLonggetLong(Objectobj,longdef){
Stringstr=obj==null?"":obj.toString();
Longl=null;
if(str.trim().length()==0){
l=newLong(def);
}
else{
try{
l=Long.valueOf(str);
}
catch(Exceptione){
}
}
returnl==null?newLong(def):l;
}
/**
*字符串转换为Integer
*@paramstr字符串
*@returnInteger,str为null时返回0
*/
publicstaticintgetIntegerValue(Objectobj){
returngetIntegerValue(obj,0);
}
/**
*字符串转换为Integer
*@paramstr字符串
*@paramdef默认值
*@returnInteger,字符串为null时返回def
*/
publicstaticintgetIntegerValue(Objectobj,intdef){
returngetInteger(obj,def).intValue();
}
/**
*字符串转换为Long
*@paramstr字符串
*@returnLong,str为null时返回0
*/
publicstaticlonggetLongValue(Objectobj){
returngetLongValue(obj,0);
}
/**
*字符串转换为Long
*@paramstr字符串
*@paramdef默认值
*@returnLong,字符串为null时返回def
*/
publicstaticlonggetLongValue(Objectobj,longdef){
returngetLong(obj,def).longValue();
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短