一个Java中有用的NumberUtil类

一、Java中NumberUtil工具

在Java开发中,经常需要进行数字的处理和转换。NumberUtil工具类提供了一些实用的方法来方便地进行数字相关的操作。

二、Java中数字处理的一般方法

在Java中,数字处理通常包括以下一些常见的操作:

  1. 数字与字符串之间的转换,例如将字符串转换为整数、长整数、浮点数等。
  2. 数字的默认值处理,当转换失败时返回默认值。
  3. 数字的进制转换,如将数字转换为特定进制的字符串表示。

三、实现内容介绍

NumberUtil工具类继承自org.springframework.util.NumberUtils,并提供了一些额外的方法来满足数字处理的需求。

四、爆红的全部方法

  1. toInt方法:有两个重载形式,一个将字符串转换为整数,若转换失败则返回0;另一个在转换失败时返回指定的默认值。
  2. toLong方法:与toInt方法类似,将字符串转换为长整数。
  3. toDouble方法:将字符串转换为Double类型,可指定默认值。
  4. toFloat方法:将字符串转换为Float类型,可指定默认值。
  5. to62String方法:将长整数转换为62进制的短字符串。

五、总结

NumberUtil工具类提供了一些方便的方法来处理数字与字符串之间的转换,以及数字的默认值处理和进制转换。在实际开发中,可以根据具体需求使用这些方法,提高数字处理的效率和准确性。例如,在需要将用户输入的字符串转换为数字时,可以使用toInttoLong方法,并处理转换失败的情况。同时,to62String方法在特定场景下,如生成短字符串标识符时,可能会非常有用。总的来说,NumberUtil工具类为Java中的数字处理提供了一些实用的功能。

五、源码

java 复制代码
import org.springframework.lang.Nullable;

/**
 * 数字类型工具类
 *
 * @author xxx
 */
public class NumberUtil extends org.springframework.util.NumberUtils {

	//-----------------------------------------------------------------------

	/**
	 * <p>Convert a <code>String</code> to an <code>int</code>, returning
	 * <code>zero</code> if the conversion fails.</p>
	 *
	 * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
	 *
	 * <pre>
	 *   NumberUtil.toInt(null) = 0
	 *   NumberUtil.toInt("")   = 0
	 *   NumberUtil.toInt("1")  = 1
	 * </pre>
	 *
	 * @param str the string to convert, may be null
	 * @return the int represented by the string, or <code>zero</code> if
	 * conversion fails
	 */
	public static int toInt(final String str) {
		return toInt(str, -1);
	}

	/**
	 * <p>Convert a <code>String</code> to an <code>int</code>, returning a
	 * default value if the conversion fails.</p>
	 *
	 * <p>If the string is <code>null</code>, the default value is returned.</p>
	 *
	 * <pre>
	 *   NumberUtil.toInt(null, 1) = 1
	 *   NumberUtil.toInt("", 1)   = 1
	 *   NumberUtil.toInt("1", 0)  = 1
	 * </pre>
	 *
	 * @param str          the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the int represented by the string, or the default if conversion fails
	 */
	public static int toInt(@Nullable final String str, final int defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Integer.valueOf(str);
		} catch (final NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>long</code>, returning
	 * <code>zero</code> if the conversion fails.</p>
	 *
	 * <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
	 *
	 * <pre>
	 *   NumberUtil.toLong(null) = 0L
	 *   NumberUtil.toLong("")   = 0L
	 *   NumberUtil.toLong("1")  = 1L
	 * </pre>
	 *
	 * @param str the string to convert, may be null
	 * @return the long represented by the string, or <code>0</code> if
	 * conversion fails
	 */
	public static long toLong(final String str) {
		return toLong(str, 0L);
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>long</code>, returning a
	 * default value if the conversion fails.</p>
	 *
	 * <p>If the string is <code>null</code>, the default value is returned.</p>
	 *
	 * <pre>
	 *   NumberUtil.toLong(null, 1L) = 1L
	 *   NumberUtil.toLong("", 1L)   = 1L
	 *   NumberUtil.toLong("1", 0L)  = 1L
	 * </pre>
	 *
	 * @param str          the string to convert, may be null
	 * @param defaultValue the default value
	 * @return the long represented by the string, or the default if conversion fails
	 */
	public static long toLong(@Nullable final String str, final long defaultValue) {
		if (str == null) {
			return defaultValue;
		}
		try {
			return Long.valueOf(str);
		} catch (final NumberFormatException nfe) {
			return defaultValue;
		}
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>Double</code>
	 *
	 * @param value value
	 * @return double value
	 */
	public static Double toDouble(String value) {
		return toDouble(value, null);
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>Double</code>
	 *
	 * @param value value
	 * @param defaultValue 默认值
	 * @return double value
	 */
	public static Double toDouble(@Nullable String value, Double defaultValue) {
		if (value != null) {
			return Double.valueOf(value.trim());
		}
		return defaultValue;
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>Double</code>
	 *
	 * @param value value
	 * @return double value
	 */
	public static Float toFloat(String value) {
		return toFloat(value, null);
	}

	/**
	 * <p>Convert a <code>String</code> to a <code>Double</code>
	 *
	 * @param value value
	 * @param defaultValue 默认值
	 * @return double value
	 */
	public static Float toFloat(@Nullable String value, Float defaultValue) {
		if (value != null) {
			return Float.valueOf(value.trim());
		}
		return defaultValue;
	}

	/**
	 * All possible chars for representing a number as a String
	 */
	private final static char[] DIGITS = {
		'0', '1', '2', '3', '4', '5',
		'6', '7', '8', '9', 'a', 'b',
		'c', 'd', 'e', 'f', 'g', 'h',
		'i', 'j', 'k', 'l', 'm', 'n',
		'o', 'p', 'q', 'r', 's', 't',
		'u', 'v', 'w', 'x', 'y', 'z',
		'A', 'B', 'C', 'D', 'E', 'F',
		'G', 'H', 'I', 'J', 'K', 'L',
		'M', 'N', 'O', 'P', 'Q', 'R',
		'S', 'T', 'U', 'V', 'W', 'X',
		'Y', 'Z'
	};

	/**
	 * 将 long 转短字符串 为 62 进制
	 *
	 * @param i 数字
	 * @return 短字符串
	 */
	public static String to62String(long i) {
		int radix = DIGITS.length;
		char[] buf = new char[65];
		int charPos = 64;
		i = -i;
		while (i <= -radix) {
			buf[charPos--] = DIGITS[(int) (-(i % radix))];
			i = i / radix;
		}
		buf[charPos] = DIGITS[(int) (-i)];

		return new String(buf, charPos, (65 - charPos));
	}
}
相关推荐
拾木20014 分钟前
常见的限流算法
java·开发语言
处处清欢18 分钟前
MaintenanceController
java·开发语言
飞翔的佩奇31 分钟前
Java项目: 基于SpringBoot+mybatis+maven校园资料分享平台(含源码+数据库+答辩PPT+毕业论文)
java·spring boot·spring·毕业设计·maven·mybatis·校园资料分享平台
不平衡的叉叉树34 分钟前
Java对象列表属性映射工具类
java
缘友一世1 小时前
mac系统安装最新(截止2024.9.13)Oracle JDK操作记录
java·macos·oracle
跃ZHD1 小时前
BolckingQueue
java
西岭千秋雪_1 小时前
谷粒商城のElasticsearch
java·大数据·服务器·spring boot·elasticsearch·搜索引擎
yueqingll2 小时前
020、二级Java选择题综合知识点(持续更新版)
java·开发语言
许野平2 小时前
Rust:深入浅出说一说 Error 类型
java·开发语言·rust·error
jingling5552 小时前
后端开发刷题 | 数字字符串转化成IP地址
java·开发语言·javascript·算法