一行代码处理字符串转数值
用法:int testNum = NumberUtil.toInt("1");
import java.text.DecimalFormat;
/**
* @author Ts
* @date 2020/5/14 16:52
* @description 数值常用类
*/
public class NumberUtil {
/**
* Instantiates a new Number util.
*/
public NumberUtil() {
super();
}
/**
* Convert a String
to a double
, returning
* 0.0d
if the conversion fails.
* If the string str
is null
,
* 0.0d
is returned.
*
* NumberUtils.toDouble(null) = 0.0d
* NumberUtils.toDouble("") = 0.0d
* NumberUtils.toDouble("1.5") = 1.5d
*
*
* @param str the string to convert, may be null
* @return the double represented by the string, or 0.0d
if conversion fails
* @since 2.1
*/
public static double toDouble(String str) {
return toDouble(str, 0.00d);
}
/**
* Convert a String
to a double
, returning a
* default value if the conversion fails.
* If the string str
is null
, the default
* value is returned.
*
* NumberUtils.toDouble(null, 1.1d) = 1.1d
* NumberUtils.toDouble("", 1.1d) = 1.1d
* NumberUtils.toDouble("1.5", 0.0d) = 1.5d
*
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the double represented by the string, or defaultValue if conversion fails
* @since 2.1
*/
public static double toDouble(String str, double defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Double.parseDouble(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
/**
* Convert a String
to a int
, returning
* 0
if the conversion fails.
* If the string str
is null
,
* 0
is returned.
*
* NumberUtils.toInt(null) = 0
* NumberUtils.toInt("") = 0
* NumberUtils.toInt("1") = 1
*
*
* @param str the string to convert, may be null
* @return the int represented by the string, or 0
if conversion fails
* @since 2.1
*/
public static int toInt(String str) {
return toInt(str, 0);
}
/**
* Convert a String
to a int
, returning a
* default value if the conversion fails.
* If the string str
is null
, the default
* value is returned.
*
* NumberUtils.toInt(null, 0) = 0
* NumberUtils.toInt("", 0) = 0
* NumberUtils.toInt("1", 0) = 1
*
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or defaultValue if conversion fails
* @since 2.1
*/
public static int toInt(String str, int defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Integer.parseInt(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
public static long toLong(String string) {
return toLong(string, 0);
}
public static long toLong(String str, long defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Long.parseLong(str);
} catch (NumberFormatException nfe) {
return defaultValue;
}
}
// Max in array
//--------------------------------------------------------------------
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static long max(long[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
long max = array[0];
for (int j = 1; j < array.length; j++) {
if (array[j] > max) {
max = array[j];
}
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static int max(int[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
int max = array[0];
for (int j = 1; j < array.length; j++) {
if (array[j] > max) {
max = array[j];
}
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static short max(short[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
short max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static byte max(byte[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
byte max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static double max(double[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
double max = array[0];
for (int j = 1; j < array.length; j++) {
if (Double.isNaN(array[j])) {
return Double.NaN;
}
if (array[j] > max) {
max = array[j];
}
}
return max;
}
/**
* Returns the maximum value in an array.
*
* @param array an array, must not be null or empty
* @return the minimum value in the array
* @throws IllegalArgumentException if array
is null
* @throws IllegalArgumentException if array
is empty
*/
public static float max(float[] array) {
// Validates input
if (array == null) {
throw new IllegalArgumentException("The Array must not be null");
} else if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty.");
}
// Finds and returns max
float max = array[0];
for (int j = 1; j < array.length; j++) {
if (Float.isNaN(array[j])) {
return Float.NaN;
}
if (array[j] > max) {
max = array[j];
}
}
return max;
}
// 3 param max
//-----------------------------------------------------------------------
/**
* Gets the maximum of three long
values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static long max(long a, long b, long c) {
if (b > a) {
a = b;
}
if (c > a) {
a = c;
}
return a;
}
/**
* Gets the maximum of three int
values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static int max(int a, int b, int c) {
if (b > a) {
a = b;
}
if (c > a) {
a = c;
}
return a;
}
/**
* Gets the maximum of three short
values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static short max(short a, short b, short c) {
if (b > a) {
a = b;
}
if (c > a) {
a = c;
}
return a;
}
/**
* Gets the maximum of three byte
values.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static byte max(byte a, byte b, byte c) {
if (b > a) {
a = b;
}
if (c > a) {
a = c;
}
return a;
}
/**
* Gets the maximum of three double
values.
* If any value is NaN
, NaN
is
* returned. Infinity is handled.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static double max(double a, double b, double c) {
return Math.max(Math.max(a, b), c);
}
/**
* Gets the maximum of three float
values.
* If any value is NaN
, NaN
is
* returned. Infinity is handled.
*
* @param a value 1
* @param b value 2
* @param c value 3
* @return the largest of the values
*/
public static float max(float a, float b, float c) {
return Math.max(Math.max(a, b), c);
}
/**
* check float equals
*
* @param a the a
* @param b the b
* @return boolean
*/
public static boolean floatEquals(float a, float b) {
return (Float.compare(a, b) == 0);
}
/**
* check double equals
*
* @param a the a
* @param b the b
* @return boolean
*/
public static boolean doubleEquals(double a, double b) {
return (Double.compare(a, b) == 0);
}
/**
* 如果是整数,不显示小数点,如果小数点有一位,则保留成两位
*
* @param d the d
* @return the string
*/
public static String doubleFormat(double d) {
return doubleEquals(d, (double) (int) d) ? String.valueOf((int) d) :
new DecimalFormat("0.00").format(d);
}
/**
* 1.00 ->1
* 1.0 ->1
* 1.10 ->1.1
*
* @param d the d
* @return string
*/
public static String doubleFormatDouble(double d) {
String s = new DecimalFormat("0.00").format(d) + "";
if (s.indexOf(".") > 0) {
//正则表达
s = s.replaceAll("0+?$", "");//去掉后面无用的零
s = s.replaceAll("[.]$", "");//如小数点后面全是零则去掉小数点
}
return s;
}
}