Class OpenNumber
java.lang.Object
cloud.opencode.base.core.OpenNumber
Number Utility Class - Validation, parsing, conversion, formatting and arithmetic operations
数值工具类 - 验证、解析、转换、格式化和算术运算
Provides comprehensive number operations including validation, parsing, conversion, formatting and range control.
提供全面的数值操作,包括验证、解析、转换、格式化和范围控制。参考 Guava Ints/Longs、Commons NumberUtils。
Features | 主要功能:
- Validation (isNumber, isInteger, isDouble, isParsable) - 验证
- Parsing with default value (toInt, toLong, toDouble) - 带默认值解析
- Safe parsing with Optional (tryParseInt, tryParseLong) - 安全解析
- Overflow-safe conversion (saturatedCast, checkedCast) - 溢出安全转换
- Range control (clamp, inRange) - 范围控制
- High-precision arithmetic (add, subtract, multiply, divide) - 高精度运算
- Formatting (format, formatPercent, formatMoney) - 格式化
Usage Examples | 使用示例:
// Validation - 验证
boolean isNum = OpenNumber.isNumber("123.45");
// Parsing - 解析
int value = OpenNumber.toInt("123", 0);
OptionalInt opt = OpenNumber.tryParseInt("123");
// Range control - 范围控制
int clamped = OpenNumber.clamp(value, 0, 100);
// High-precision arithmetic - 高精度运算
BigDecimal result = OpenNumber.add(a, b, c);
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是 (无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalHigh-precision addition of multiple Number values.static intcheckedCast(long value) Converts a long to an int, throwing ArithmeticException on overflow.static doubleclamp(double value, double min, double max) Clamps the double value to the range [min, max].static intclamp(int value, int min, int max) Clamps the value to the range [min, max].static longclamp(long value, long min, long max) Clamps the long value to the range [min, max].static intcompare(double x, double y) Compares two double values.static intcompare(int x, int y) Compares two int values.static intcompare(long x, long y) Compares two long values.static intconstrainToRange(int value, int min, int max) Constrains the value to the range [min, max] (alias for clamp).static BigDecimaldivide(BigDecimal a, BigDecimal b, int scale) High-precision division with half-up rounding.static BigDecimaldivide(BigDecimal a, BigDecimal b, int scale, RoundingMode mode) High-precision division with specified rounding mode.static StringFormats a double value using the given pattern.static Stringformat(BigDecimal value, String pattern) Formats a BigDecimal value using the given pattern.static StringformatMoney(BigDecimal value) Formats a BigDecimal value as a currency string using the JVM's default Locale.static StringformatPercent(double value, int scale) Formats a double value as a percentage string with the given number of decimal places.static booleaninRange(int value, int min, int max) Returns true if the value is within the range [min, max].static booleaninRange(long value, long min, long max) Returns true if the long value is within the range [min, max].static booleanisCreatable(String str) Returns true if the string can be created as a Number (including hex and octal).static booleanReturns true if the string can be parsed as a double value.static booleanReturns true if the string represents a valid integer.static booleanReturns true if the string can be parsed as a long value.static booleanstatic booleanisParsable(String str) Returns true if the string can be parsed as a decimal number (no hex/octal).static intmax(int... array) Returns the maximum value in the int array.static longmax(long... array) Returns the maximum value in the long array.static <T extends Comparable<T>>
Tmax(T a, T b) Returns the larger of two Comparable values.static intmin(int... array) Returns the minimum value in the int array.static longmin(long... array) Returns the minimum value in the long array.static <T extends Comparable<T>>
Tmin(T a, T b) Returns the smaller of two Comparable values.static BigDecimalmultiply(BigDecimal a, BigDecimal b) High-precision multiplication.static doubleround(double value, int scale) Rounds the double value to the specified number of decimal places.static BigDecimalround(BigDecimal value, int scale) Rounds the BigDecimal value to the specified scale using half-up rounding.static BigDecimalround(BigDecimal value, int scale, RoundingMode mode) Rounds the BigDecimal value to the specified scale with the given rounding mode.static BigDecimalroundHalfEven(BigDecimal value, int scale) Rounds the BigDecimal value using banker's rounding (HALF_EVEN).static intsaturatedCast(long value) Converts a long to an int, clamping to Integer.MAX_VALUE or Integer.MIN_VALUE on overflow.static intsaturatedCast(BigDecimal value) Converts a BigDecimal to an int, clamping on overflow.static BigDecimalsubtract(BigDecimal a, BigDecimal b) High-precision subtraction.static BigDecimaltoBigDecimal(String str) Parses the string as a BigDecimal, returning null on failure.static BigDecimaltoBigDecimal(String str, BigDecimal defaultValue) Parses the string as a BigDecimal, returning the default value on failure.static BigIntegertoBigInteger(String str) Parses the string as a BigInteger, returning null on failure.static doubleParses the string as a double, returning the default value on failure.static floatParses the string as a float, returning the default value on failure.static intParses the string as an int, returning the default value on failure.static longParses the string as a long, returning the default value on failure.static OptionalDoubletryParseDouble(String str) Tries to parse the string as a double, returning OptionalDouble.empty() on failure.static OptionalInttryParseInt(String str) Tries to parse the string as an int, returning OptionalInt.empty() on failure.static OptionalLongtryParseLong(String str) Tries to parse the string as a long, returning OptionalLong.empty() on failure.
-
Method Details
-
isNumber
-
isInteger
Returns true if the string represents a valid integer. 检查字符串是否为整数- Parameters:
str- the string | 字符串- Returns:
- true if it is an integer | 如果是整数返回 true
-
isLong
Returns true if the string can be parsed as a long value. 检查字符串是否为 long 类型整数- Parameters:
str- the string | 字符串- Returns:
- true if parsable as long | 如果可解析为 long 返回 true
-
isDouble
Returns true if the string can be parsed as a double value. 检查字符串是否为 double 类型浮点数- Parameters:
str- the string | 字符串- Returns:
- true if parsable as double | 如果可解析为 double 返回 true
-
isCreatable
Returns true if the string can be created as a Number (including hex and octal). 检查字符串是否可以创建为 Number- Parameters:
str- the string | 字符串- Returns:
- true if creatable as Number | 如果可以创建为 Number 返回 true
-
isParsable
Returns true if the string can be parsed as a decimal number (no hex/octal). 检查字符串是否可解析为数字- Parameters:
str- the string | 字符串- Returns:
- true if parsable | 如果可以解析返回 true
-
toInt
Parses the string as an int, returning the default value on failure. 解析字符串为 int- Parameters:
str- the string | 字符串defaultValue- default value | 默认值- Returns:
- parsed value or default | 解析结果或默认值
-
toLong
Parses the string as a long, returning the default value on failure. 解析字符串为 long- Parameters:
str- the string | 字符串defaultValue- default value | 默认值- Returns:
- parsed value or default | 解析结果或默认值
-
toFloat
Parses the string as a float, returning the default value on failure. 解析字符串为 float- Parameters:
str- the string | 字符串defaultValue- default value | 默认值- Returns:
- parsed value or default | 解析结果或默认值
-
toDouble
Parses the string as a double, returning the default value on failure. 解析字符串为 double- Parameters:
str- the string | 字符串defaultValue- default value | 默认值- Returns:
- parsed value or default | 解析结果或默认值
-
toBigDecimal
Parses the string as a BigDecimal, returning null on failure. 解析字符串为 BigDecimal- Parameters:
str- the string | 字符串- Returns:
- BigDecimal, or null if parsing fails | BigDecimal,如果解析失败返回 null
-
toBigDecimal
Parses the string as a BigDecimal, returning the default value on failure. 解析字符串为 BigDecimal- Parameters:
str- the string | 字符串defaultValue- default value | 默认值- Returns:
- BigDecimal or default | BigDecimal 或默认值
-
toBigInteger
Parses the string as a BigInteger, returning null on failure. 解析字符串为 BigInteger- Parameters:
str- the string | 字符串- Returns:
- BigInteger, or null if parsing fails | BigInteger,如果解析失败返回 null
-
tryParseInt
Tries to parse the string as an int, returning OptionalInt.empty() on failure. 尝试解析为 int- Parameters:
str- the string | 字符串- Returns:
- OptionalInt | OptionalInt
-
tryParseLong
Tries to parse the string as a long, returning OptionalLong.empty() on failure. 尝试解析为 long- Parameters:
str- the string | 字符串- Returns:
- OptionalLong | OptionalLong
-
tryParseDouble
Tries to parse the string as a double, returning OptionalDouble.empty() on failure. 尝试解析为 double- Parameters:
str- the string | 字符串- Returns:
- OptionalDouble | OptionalDouble
-
saturatedCast
public static int saturatedCast(long value) Converts a long to an int, clamping to Integer.MAX_VALUE or Integer.MIN_VALUE on overflow. 将 long 转为 int,溢出时截断到 int 范围- Parameters:
value- long value | long 值- Returns:
- int value, or Integer.MAX_VALUE / Integer.MIN_VALUE on overflow | int 值,溢出时返回 Integer.MAX_VALUE 或 Integer.MIN_VALUE
-
checkedCast
public static int checkedCast(long value) Converts a long to an int, throwing ArithmeticException on overflow. 将 long 转为 int,溢出时抛出异常- Parameters:
value- long value | long 值- Returns:
- int value | int 值
- Throws:
ArithmeticException- if overflow | 如果溢出
-
saturatedCast
Converts a BigDecimal to an int, clamping on overflow. 将 BigDecimal 转为 int,溢出时截断- Parameters:
value- BigDecimal value | BigDecimal 值- Returns:
- int value | int 值
-
compare
public static int compare(int x, int y) Compares two int values. 比较两个 int 值- Parameters:
x- first value | 值1y- second value | 值2- Returns:
- comparison result | 比较结果
-
compare
public static int compare(long x, long y) Compares two long values. 比较两个 long 值- Parameters:
x- first value | 值1y- second value | 值2- Returns:
- comparison result | 比较结果
-
compare
public static int compare(double x, double y) Compares two double values. 比较两个 double 值- Parameters:
x- first value | 值1y- second value | 值2- Returns:
- comparison result | 比较结果
-
max
Returns the larger of two Comparable values. 返回两个值中的较大值- Type Parameters:
T- value type | 值类型- Parameters:
a- first value | 值1b- second value | 值2- Returns:
- the larger value | 较大值
-
min
Returns the smaller of two Comparable values. 返回两个值中的较小值- Type Parameters:
T- value type | 值类型- Parameters:
a- first value | 值1b- second value | 值2- Returns:
- the smaller value | 较小值
-
max
public static int max(int... array) Returns the maximum value in the int array. 返回数组中的最大值- Parameters:
array- int array | int 数组- Returns:
- maximum value | 最大值
- Throws:
IllegalArgumentException- if array is empty | 如果数组为空
-
min
public static int min(int... array) Returns the minimum value in the int array. 返回数组中的最小值- Parameters:
array- int array | int 数组- Returns:
- minimum value | 最小值
- Throws:
IllegalArgumentException- if array is empty | 如果数组为空
-
max
public static long max(long... array) Returns the maximum value in the long array. 返回 long 数组中的最大值 -
min
public static long min(long... array) Returns the minimum value in the long array. 返回 long 数组中的最小值 -
clamp
public static int clamp(int value, int min, int max) Clamps the value to the range [min, max]. 限制值在指定范围内- Parameters:
value- the value | 值min- minimum value | 最小值max- maximum value | 最大值- Returns:
- value clamped to range | 范围内的值
-
clamp
public static long clamp(long value, long min, long max) Clamps the long value to the range [min, max]. 限制值在指定范围内 -
clamp
public static double clamp(double value, double min, double max) Clamps the double value to the range [min, max]. 限制值在指定范围内 -
constrainToRange
public static int constrainToRange(int value, int min, int max) Constrains the value to the range [min, max] (alias for clamp). 限制值在指定范围内(Guava 风格别名) -
inRange
public static boolean inRange(int value, int min, int max) Returns true if the value is within the range [min, max]. 检查值是否在范围内 [min, max]- Parameters:
value- the value | 值min- minimum value | 最小值max- maximum value | 最大值- Returns:
- true if in range | 如果在范围内返回 true
-
inRange
public static boolean inRange(long value, long min, long max) Returns true if the long value is within the range [min, max]. 检查值是否在范围内 [min, max] -
add
High-precision addition of multiple Number values. 高精度加法- Parameters:
values- number values | 数值数组- Returns:
- sum | 和
-
subtract
High-precision subtraction. 高精度减法 -
multiply
High-precision multiplication. 高精度乘法 -
divide
High-precision division with half-up rounding. 高精度除法- Parameters:
a- dividend | 被除数b- divisor | 除数scale- number of decimal places | 小数位数- Returns:
- quotient | 商
-
divide
High-precision division with specified rounding mode. 高精度除法- Parameters:
a- dividend | 被除数b- divisor | 除数scale- number of decimal places | 小数位数mode- rounding mode | 舍入模式- Returns:
- quotient | 商
-
round
Rounds the BigDecimal value to the specified scale using half-up rounding. 四舍五入- Parameters:
value- BigDecimal value | BigDecimal 值scale- number of decimal places | 小数位数- Returns:
- rounded value | 四舍五入后的值
-
round
Rounds the BigDecimal value to the specified scale with the given rounding mode. 四舍五入- Parameters:
value- BigDecimal value | BigDecimal 值scale- number of decimal places | 小数位数mode- rounding mode | 舍入模式- Returns:
- rounded value | 舍入后的值
-
round
public static double round(double value, int scale) Rounds the double value to the specified number of decimal places. 四舍五入- Parameters:
value- double value | double 值scale- number of decimal places | 小数位数- Returns:
- rounded value | 四舍五入后的值
-
roundHalfEven
Rounds the BigDecimal value using banker's rounding (HALF_EVEN). 银行家舍入(四舍六入五成双)- Parameters:
value- BigDecimal value | BigDecimal 值scale- number of decimal places | 小数位数- Returns:
- rounded value | 舍入后的值
-
format
-
format
Formats a BigDecimal value using the given pattern. 格式化数值- Parameters:
value- BigDecimal value | BigDecimal 值pattern- format pattern | 格式模式- Returns:
- formatted string | 格式化后的字符串
-
formatPercent
Formats a double value as a percentage string with the given number of decimal places. 格式化为百分比- Parameters:
value- double value | double 值scale- number of decimal places | 小数位数- Returns:
- percentage string | 百分比字符串
-
formatMoney
Formats a BigDecimal value as a currency string using the JVM's default Locale. 使用 JVM 默认 Locale 将 BigDecimal 值格式化为货币字符串。- Parameters:
value- BigDecimal value | BigDecimal 值- Returns:
- currency string | 货币字符串
- API Note:
- The output depends on the JVM's default Locale (
Locale.getDefault()). Results may differ across servers with different Locale settings. For explicit Locale control, useNumberFormat.getCurrencyInstance(java.util.Locale). 输出取决于 JVM 默认 Locale,不同服务器上的结果可能不同。 如需显式指定 Locale,请使用NumberFormat.getCurrencyInstance(java.util.Locale)。
-