java字符串和数字转换工具

[b]java数字和字符串之间的转换工具[/b]
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
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值