public final class BigDecimalTools
extends java.lang.Object
BigDecimal and related objects.| Modifier and Type | Field and Description |
|---|---|
static org.apache.log4j.Logger |
log |
| Constructor and Description |
|---|
BigDecimalTools() |
| Modifier and Type | Method and Description |
|---|---|
static java.math.BigDecimal |
chapterStringToBigDecimal(java.lang.String chapterString,
int maxDigitsPerSegment)
Converts a chapter string (e.g., "1.2", "1,23", "3.5.1") to a BigDecimal, maintaining the intended order by padding each chapter
segment with leading zeros.
|
static boolean |
equalsByCompare(java.math.BigDecimal[] a,
java.math.BigDecimal[] b)
Returns true if the two specified arrays of BigDecimals are equal to one another.
|
static int |
getOrderOfMagnitude(java.math.BigDecimal bd)
Returns the order of magnitude, i.e., the nearest power of ten, for the given BigDecimal.
|
static boolean |
isWithinTolerance(java.math.BigDecimal number,
java.math.BigDecimal centralValue,
java.math.BigDecimal tolerance)
Checks whether a given BigDecimal is within a specified tolerance or margin of error from a central value.
|
static boolean |
liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
Returns true if
n is in the closed interval [lowerBound, upperBound]. |
static boolean |
liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound,
boolean excludingLeft)
Returns true if
n is in the closed interval [lowerBound, upperBound]. |
static boolean |
liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound,
boolean excludingLeft,
boolean excludingRight)
Returns true if
n is in the closed interval [lowerBound, upperBound]. |
static boolean |
liesInLeftOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
Returns true if
n is in the left open interval (lowerBound, upperBound]. |
static boolean |
liesInOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
Returns true if
n is in the open interval (lowerBound, upperBound). |
static boolean |
liesInRightOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
Returns true if
n is in the right open interval [lowerBound, upperBound). |
static java.math.BigDecimal |
normalize(java.math.BigDecimal val)
Normalizes a given
BigDecimal by removing trailing zeros while preserving its original scale. |
static java.math.BigDecimal |
normalize(java.math.BigDecimal val,
int maxScale)
Normalizes a given
BigDecimal by capping its scale and removing trailing zeros. |
static java.math.BigDecimal |
roundToNearestOrderOfMagnitude(java.math.BigDecimal bd)
Rounds the passed BigDecimal to the nearest order of magnitude, i.e., power of ten.
|
static java.math.BigDecimal |
roundToNextOrderOfMagnitude(java.math.BigDecimal bd)
Rounds the passed BigDecimal to the next order of magnitude, i.e., power of ten.
|
static java.math.BigDecimal |
roundUpToGivenFraction(java.math.BigDecimal val,
java.math.BigDecimal ceiling)
Rounds the provided value
val to the specified ceiling. |
static java.math.BigDecimal |
sqrt(java.math.BigDecimal value)
Calculates an approximation of the square root for the passed BigDecimal to a precision of around 30 total digits.
|
static java.math.BigDecimal |
sum(java.math.BigDecimal[] array) |
static java.math.BigDecimal |
sum(java.lang.Iterable<java.math.BigDecimal> iter) |
static java.math.BigDecimal |
toBigDecimal(java.lang.Number number)
Creates a BigDecimal from a Number.
|
public static final java.math.BigDecimal toBigDecimal(java.lang.Number number)
number - the Number to generate the BigDecimal frompublic static final boolean equalsByCompare(java.math.BigDecimal[] a,
java.math.BigDecimal[] b)
Arrays.equals(Object[], Object[]), only that it uses BigDecimal.compareTo(java.math.BigDecimal)
for the equality check.a - one array tested for compare equality, nullable.b - the other array tested for compare equality, nullable.public static final java.math.BigDecimal sum(java.math.BigDecimal[] array)
public static final java.math.BigDecimal sum(java.lang.Iterable<java.math.BigDecimal> iter)
public static final boolean isWithinTolerance(java.math.BigDecimal number,
java.math.BigDecimal centralValue,
java.math.BigDecimal tolerance)
number - the BigDecimal to check.centralValue - the central value to use as a reference point.tolerance - the tolerance value that determines the allowed deviation from the central value.java.lang.IllegalArgumentException - if the number, centralValue, or tolerance parameters are null or the tolerance parameter is negative.public static final boolean liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
n is in the closed interval [lowerBound, upperBound].
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervaln is in the specified intervaljava.lang.IllegalArgumentException - if the input is invalidpublic static final boolean liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound,
boolean excludingLeft)
n is in the closed interval [lowerBound, upperBound].
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervalexcludingLeft - whether to exclude the left endpointn is in the specified intervaljava.lang.IllegalArgumentException - if the input is invalidpublic static final boolean liesInInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound,
boolean excludingLeft,
boolean excludingRight)
n is in the closed interval [lowerBound, upperBound].
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervalexcludingLeft - whether to exclude the left endpointexcludingRight - whether to exclude the right endpointn is in the specified intervaljava.lang.IllegalArgumentException - if the input is invalidpublic static final boolean liesInOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
n is in the open interval (lowerBound, upperBound).
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervaln is in the specified open intervalpublic static final boolean liesInLeftOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
n is in the left open interval (lowerBound, upperBound].
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervaln is in the specified left open intervalpublic static final boolean liesInRightOpenInterval(java.math.BigDecimal n,
java.math.BigDecimal lowerBound,
java.math.BigDecimal upperBound)
n is in the right open interval [lowerBound, upperBound).
Null values for lowerBound or upperBound represent infinity.n - the BigDecimal to checklowerBound - the lower bound of the intervalupperBound - the upper bound of the intervaln is in the specified right open intervalpublic static final java.math.BigDecimal sqrt(java.math.BigDecimal value)
value - the BigDecimal for which to calculate the square rootpublic static final java.math.BigDecimal roundUpToGivenFraction(java.math.BigDecimal val,
java.math.BigDecimal ceiling)
val to the specified ceiling.val - the value to round upceiling - the ceiling to round up tojava.lang.IllegalStateException - if the input value is negativepublic static final int getOrderOfMagnitude(java.math.BigDecimal bd)
bd - the BigDecimal for which to determine the order of magnitudejava.lang.IllegalArgumentException - if the input is negativepublic static final java.math.BigDecimal roundToNearestOrderOfMagnitude(java.math.BigDecimal bd)
bd - the BigDecimal to roundpublic static final java.math.BigDecimal roundToNextOrderOfMagnitude(java.math.BigDecimal bd)
bd - the BigDecimal to roundpublic static final java.math.BigDecimal normalize(java.math.BigDecimal val)
BigDecimal by removing trailing zeros while preserving its original scale.
This method is a convenience variant of normalize(BigDecimal, int)
that retains the original scale of the provided BigDecimal.
Operations performed:
BigDecimal to ensure a cleaner representation.Important Considerations:
String, especially when working with very large
or very small numbers, you may encounter scientific notation (e.g., 1E+10). To avoid this, use toPlainString()
on the BigDecimal for a more predictable representation.L10n#formatDecimal(BigDecimal, String, Locale) to get a properly formatted string
representation according to a given pattern and locale.val is null, the method returns null.Example Usage:
BigDecimal value = new BigDecimal("123.45000");
BigDecimal normalizedValue = normalize(value);
System.out.println(normalizedValue); // Output: "123.45"
BigDecimal largeValue = new BigDecimal("12345000000");
BigDecimal normalizedLargeValue = normalize(largeValue);
System.out.println(normalizedLargeValue.toString()); // Output: "1.2345E+10"
System.out.println(normalizedLargeValue.toPlainString()); // Output: "12345000000"
val - the BigDecimal to normalizeBigDecimal without trailing zeros, retaining the original scale,
or null if val is nullnormalize(BigDecimal, int)public static final java.math.BigDecimal normalize(java.math.BigDecimal val,
int maxScale)
BigDecimal by capping its scale and removing trailing zeros.
This method performs two main operations on the provided BigDecimal:
BigDecimal exceeds the specified maxScale,
it rounds the value to the maxScale using BigDecimal.ROUND_HALF_UP rounding mode.BigDecimal to ensure a cleaner and normalized representation.Important Considerations:
BigDecimal.ROUND_HALF_UP for rounding when reducing the scale.
This is a standard rounding mode where values are rounded to the nearest neighbor, and ties are broken by rounding up.String, especially when working with very large
or very small numbers, you may encounter scientific notation (e.g., 1E+10). To avoid this, use toPlainString()
on the BigDecimal for a more predictable representation.L10n#formatDecimal(BigDecimal, String, Locale) to get a properly formatted string
representation according to a given pattern and locale.val is null, the method returns null.-1 as maxScale will retain the original scale of the passed BigDecimal.Example Usage:
BigDecimal value = new BigDecimal("123.54321");
BigDecimal normalizedValue = normalize(value, 2);
System.out.println(normalizedValue); // Output: "123.54"
BigDecimal largeValue = new BigDecimal("12345000000");
BigDecimal normalizedLargeValue = normalize(largeValue, -1);
System.out.println(normalizedLargeValue.toString()); // Output: "1.2345E+10"
System.out.println(normalizedLargeValue.toPlainString()); // Output: "12345000000"
val - the BigDecimal to normalizemaxScale - the maximum scale to set, use -1 to retain the original scaleBigDecimal, capped to the specified scale and without trailing zeros,
or null if val is nullpublic static java.math.BigDecimal chapterStringToBigDecimal(java.lang.String chapterString,
int maxDigitsPerSegment)
chapterString - The string representing the chapter number. Supports both dots (".") and commas (",") as separators.maxDigitsPerSegment - The maximum number of digits to consider for each chapter segment when padding with leading zeros.
This ensures correct lexicographical ordering when the resulting BigDecimal values are compared.maxDigitsPerSegment length and placed after the decimal point (e.g., "1.2" with maxDigitsPerSegment of
3 becomes 0.001002). Returns BigDecimal.ZERO if the input chapterString is null or empty.
Non-numeric parts in the chapter string will be treated leniently as 0.java.lang.IllegalArgumentException - If any chapter segment (after trimming) has more digits than maxDigitsPerSegment.Copyright © 2000-2025 OAshi S.à r.l. All Rights Reserved.