java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具

本文實(shí)例為大家分享了java字符串和數(shù)字轉(zhuǎn)換工具的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)建站基于分布式IDC數(shù)據(jù)中心構(gòu)建的平臺(tái)為眾多戶提供四川電信機(jī)房托管 四川大帶寬租用 成都機(jī)柜租用 成都服務(wù)器租用。

package com.test.util;

/**
 * 數(shù)字工具類
 */
public class NumberUtil {

 /**
  * 數(shù)字轉(zhuǎn)換為字符串
  * @param num 數(shù)字
  * @return 字符串,如果 num 為空, 返回空字符串
  */
 public static String num2Str(Object num) {
  String str = null;

  if (num == null) {
   str = "";
  }
  else {
   str = String.valueOf(num);
  }
  return str;
 }

 /**
  * 字符串轉(zhuǎn)換為Integer
  * @param str 字符串
  * @return Integer, str為null時(shí)返回0
  */
 public static Integer getInteger(Object obj) {
  return getInteger(obj, 0);
 }

 /**
  * 字符串轉(zhuǎn)換為Integer
  * @param str 字符串
  * @param def 默認(rèn)值
  * @return Integer, 字符串為null時(shí)返回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;
 }

 /**
  * 字符串轉(zhuǎn)換為Long
  * @param str 字符串
  * @return Long, str為null時(shí)返回0
  */
 public static Long getLong(Object obj) {
  return getLong(obj, 0);
 }

 /**
  * 字符串轉(zhuǎn)換為Long
  * @param str 字符串
  * @param def 默認(rèn)值
  * @return Long, 字符串為null時(shí)返回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;
 }

 /**
  * 字符串轉(zhuǎn)換為Integer
  * @param str 字符串
  * @return Integer, str為null時(shí)返回0
  */
 public static int getIntegerValue(Object obj) {
  return getIntegerValue(obj, 0);
 }

 /**
  * 字符串轉(zhuǎn)換為Integer
  * @param str 字符串
  * @param def 默認(rèn)值
  * @return Integer, 字符串為null時(shí)返回def
  */
 public static int getIntegerValue(Object obj, int def) {
  return getInteger(obj, def).intValue();
 }

 /**
  * 字符串轉(zhuǎn)換為Long
  * @param str 字符串
  * @return Long, str為null時(shí)返回0
  */
 public static long getLongValue(Object obj) {
  return getLongValue(obj, 0);
 }

 /**
  * 字符串轉(zhuǎn)換為Long
  * @param str 字符串
  * @param def 默認(rèn)值
  * @return Long, 字符串為null時(shí)返回def
  */
 public static long getLongValue(Object obj, long def) {
  return getLong(obj, def).longValue();
 }
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

本文名稱:java實(shí)現(xiàn)字符串和數(shù)字轉(zhuǎn)換工具
分享鏈接:http://muchs.cn/article44/pgophe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信公眾號(hào)、ChatGPT、小程序開發(fā)、電子商務(wù)、響應(yīng)式網(wǎng)站、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)