一、Java中NumberUtil工具
在Java开发中,经常需要进行数字的处理和转换。NumberUtil
工具类提供了一些实用的方法来方便地进行数字相关的操作。
二、Java中数字处理的一般方法
在Java中,数字处理通常包括以下一些常见的操作:
- 数字与字符串之间的转换,例如将字符串转换为整数、长整数、浮点数等。
- 数字的默认值处理,当转换失败时返回默认值。
- 数字的进制转换,如将数字转换为特定进制的字符串表示。
三、实现内容介绍
NumberUtil
工具类继承自org.springframework.util.NumberUtils
,并提供了一些额外的方法来满足数字处理的需求。
四、爆红的全部方法
toInt
方法:有两个重载形式,一个将字符串转换为整数,若转换失败则返回0;另一个在转换失败时返回指定的默认值。toLong
方法:与toInt
方法类似,将字符串转换为长整数。toDouble
方法:将字符串转换为Double
类型,可指定默认值。toFloat
方法:将字符串转换为Float
类型,可指定默认值。to62String
方法:将长整数转换为62进制的短字符串。
五、总结
NumberUtil
工具类提供了一些方便的方法来处理数字与字符串之间的转换,以及数字的默认值处理和进制转换。在实际开发中,可以根据具体需求使用这些方法,提高数字处理的效率和准确性。例如,在需要将用户输入的字符串转换为数字时,可以使用toInt
或toLong
方法,并处理转换失败的情况。同时,to62String
方法在特定场景下,如生成短字符串标识符时,可能会非常有用。总的来说,NumberUtil
工具类为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));}
}