java str转换工具_java实现字符串和数字转换工具

本文实例为大家分享了java字符串和数字转换工具的具体代码,供大家参考,具体内容如下

package com.test.util;

/**

* 数字工具类

*/

public class numberutil {

/**

* 数字转换为字符串

* @param num 数字

* @return 字符串,如果 num 为空, 返回空字符串

*/

public static string num2str(object num) {

string str = null;

if (num == null) {

str = "";

}

else {

str = string.valueof(num);

}

return str;

}

/**

* 字符串转换为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 = null;

if (str.trim().length() == 0) {

i = new integer(def);

}

else {

try {

i = integer.valueof(str);

}

catch (exception e) {

}

}

return i == null ? new integer(def) : i;

}

/**

* 字符串转换为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();

long l = null;

if (str.trim().length() == 0) {

l = new long(def);

}

else {

try {

l = long.valueof(str);

}

catch (exception e) {

}

}

return l == null ? new long(def) : l;

}

/**

* 字符串转换为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();

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持萬仟网。

希望与广大网友互动??

点此进行留言吧!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值