Package com.helger.base.numeric
Class BigHelper
java.lang.Object
com.helger.base.numeric.BigHelper
BigDecimal and BigInteger helper
- Since:
- v12.0.0
- Author:
- Philip Helger
-
Method Summary
Modifier and TypeMethodDescriptionstatic @NonNull BigDecimalabs(@NonNull BigDecimal aValue) This is a sanity method wrappingBigDecimal.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigDecimal directly.static @NonNull BigIntegerabs(@NonNull BigInteger aValue) This is a sanity method wrappingBigInteger.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigInteger directly.static @NonNull BigDecimaladdPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Add x% to basestatic @NonNull BigDecimaladdPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, int nScale, @NonNull RoundingMode eRoundingMode) Add x% to basestatic booleanequalValues(@Nullable BigDecimal aObj1, @Nullable BigDecimal aObj2) Special equals implementation for BigDecimal becauseBigDecimal.equalsreturnsfalseif they have a different scale so that "5.5" is not equal "5.50".static @NonNull BigDecimalgetDividedBigDecimal(long nDividend, long nDivisor) Get the division result usingBigDecimal.static @NonNull BigDecimalgetDividedBigDecimal(long nDividend, long nDivisor, int nScale, @NonNull RoundingMode eRoundingMode) Get the division result usingBigDecimal.static intgetFractionDigits(@NonNull BigDecimal aBD) Get the number of effective fraction digits by the specified BigDecimal.static @NonNull BigDecimalgetMaxBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues) Get the maximum of the passedBigDecimalvalues.static @NonNull BigIntegergetMaxBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues) Get the maximum of the passedBigIntegervalues.static @NonNull BigDecimalgetMinBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues) Get the minimum of the passedBigDecimalvalues.static @NonNull BigIntegergetMinBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues) Get the minimum of the passedBigIntegervalues.static @NonNull BigDecimalgetPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Get x% from basestatic @NonNull BigDecimalgetPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, int nScale, @NonNull RoundingMode eRoundingMode) Get x% from base with rounding etc.static @Nullable BigDecimalgetWithoutTrailingZeroes(@Nullable String sValue) Get the passed String as a BigDecimal without any trailing zeroes.static @Nullable BigDecimalgetWithoutTrailingZeroes(@Nullable BigDecimal aValue) Get the passed BigDecimal without any trailing zeroes.static booleanisEQ0(@NonNull BigDecimal aValue) static booleanisEQ0(@NonNull BigInteger aValue) static booleanisEQ1(@NonNull BigDecimal aValue) static booleanisEQ1(@NonNull BigInteger aValue) static booleanisEQ10(@NonNull BigDecimal aValue) static booleanisEQ10(@NonNull BigInteger aValue) static booleanisEQ100(@NonNull BigDecimal aValue) static booleanisEQ100(@NonNull BigInteger aValue) static booleanisGE0(@NonNull BigDecimal aValue) static booleanisGE0(@NonNull BigInteger aValue) static booleanisGE1(@NonNull BigDecimal aValue) static booleanisGE1(@NonNull BigInteger aValue) static booleanisGE10(@NonNull BigDecimal aValue) static booleanisGE10(@NonNull BigInteger aValue) static booleanisGE100(@NonNull BigDecimal aValue) static booleanisGE100(@NonNull BigInteger aValue) static booleanisGT0(@NonNull BigDecimal aValue) static booleanisGT0(@NonNull BigInteger aValue) static booleanisGT1(@NonNull BigDecimal aValue) static booleanisGT1(@NonNull BigInteger aValue) static booleanisGT10(@NonNull BigDecimal aValue) static booleanisGT10(@NonNull BigInteger aValue) static booleanisGT100(@NonNull BigDecimal aValue) static booleanisGT100(@NonNull BigInteger aValue) static booleanisLE0(@NonNull BigDecimal aValue) static booleanisLE0(@NonNull BigInteger aValue) static booleanisLE1(@NonNull BigDecimal aValue) static booleanisLE1(@NonNull BigInteger aValue) static booleanisLE10(@NonNull BigDecimal aValue) static booleanisLE10(@NonNull BigInteger aValue) static booleanisLE100(@NonNull BigDecimal aValue) static booleanisLE100(@NonNull BigInteger aValue) static booleanisLT0(@NonNull BigDecimal aValue) static booleanisLT0(@NonNull BigInteger aValue) static booleanisLT1(@NonNull BigDecimal aValue) static booleanisLT1(@NonNull BigInteger aValue) static booleanisLT10(@NonNull BigDecimal aValue) static booleanisLT10(@NonNull BigInteger aValue) static booleanisLT100(@NonNull BigDecimal aValue) static booleanisLT100(@NonNull BigInteger aValue) static booleanisNE0(@NonNull BigDecimal aValue) static booleanisNE0(@NonNull BigInteger aValue) static booleanisNE1(@NonNull BigDecimal aValue) static booleanisNE1(@NonNull BigInteger aValue) static booleanisNE10(@NonNull BigDecimal aValue) static booleanisNE10(@NonNull BigInteger aValue) static booleanisNE100(@NonNull BigDecimal aValue) static booleanisNE100(@NonNull BigInteger aValue) static @NonNull BigDecimalsubtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Subtract x% from basestatic @NonNull BigDecimalsubtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, int nScale, @NonNull RoundingMode eRoundingMode) Subtract x% from basestatic @NonNull BigDecimaltoBigDecimal(double d) Convert a double to aBigDecimal.static @NonNull BigDecimaltoBigDecimal(float f) Convert a float to aBigDecimal.static @NonNull BigDecimaltoBigDecimal(int n) Convert an int to aBigDecimal.static @NonNull BigDecimaltoBigDecimal(long n) Convert a long to aBigDecimal.static @NonNull BigDecimaltoBigDecimal(@NonNull Number aNumber) Convert aNumberto aBigDecimal.static @NonNull BigDecimaltoBigDecimal(@NonNull String sNumber) Convert a String to aBigDecimal.static @NonNull BigIntegertoBigInteger(int n) Convert an int to aBigInteger.static @NonNull BigIntegertoBigInteger(long n) Convert a long to aBigInteger.static @NonNull BigIntegertoBigInteger(@NonNull Number aNumber) Convert aNumberto aBigInteger.static @NonNull BigIntegertoBigInteger(@NonNull String sNumber) Convert a String to aBigInteger.
-
Method Details
-
getDividedBigDecimal
Get the division result usingBigDecimal.- Parameters:
nDividend- the dividendnDivisor- the divisor- Returns:
- the result of the division
- Throws:
ArithmeticException- if the divisor is 0.
-
getDividedBigDecimal
public static @NonNull BigDecimal getDividedBigDecimal(long nDividend, long nDivisor, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode) Get the division result usingBigDecimal.- Parameters:
nDividend- the dividendnDivisor- the divisornScale- Number of fraction digits. Must be ≥ 0.eRoundingMode- Round mode to be used. May not benull.- Returns:
- the result of the division
- Throws:
ArithmeticException- if the divisor is 0.- Since:
- v11.0.2
-
getMaxBigDecimal
public static @NonNull BigDecimal getMaxBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues) Get the maximum of the passedBigDecimalvalues.- Parameters:
aValue- The first value. May not benull.aValues- The remaining values. May not benull.- Returns:
- The maximum of all passed values. Never
null.
-
getMaxBigInteger
public static @NonNull BigInteger getMaxBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues) Get the maximum of the passedBigIntegervalues.- Parameters:
aValue- The first value. May not benull.aValues- The remaining values. May not benull.- Returns:
- The maximum of all passed values. Never
null.
-
getMinBigDecimal
public static @NonNull BigDecimal getMinBigDecimal(@NonNull BigDecimal aValue, @NonNull BigDecimal... aValues) Get the minimum of the passedBigDecimalvalues.- Parameters:
aValue- The first value. May not benull.aValues- The remaining values. May not benull.- Returns:
- The minimum of all passed values. Never
null.
-
getMinBigInteger
public static @NonNull BigInteger getMinBigInteger(@NonNull BigInteger aValue, @NonNull BigInteger... aValues) Get the minimum of the passedBigIntegervalues.- Parameters:
aValue- The first value. May not benull.aValues- The remaining values. May not benull.- Returns:
- The minimum of all passed values. Never
null.
-
abs
This is a sanity method wrappingBigDecimal.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigDecimal directly.- Parameters:
aValue- Input value- Returns:
- the absolute value of the argument.
-
abs
This is a sanity method wrappingBigInteger.abs (double), so that you don't have to think whether you need to invoke the abs method from this class or the one from BigInteger directly.- Parameters:
aValue- Input value- Returns:
- the absolute value of the argument.
-
equalValues
Special equals implementation for BigDecimal becauseBigDecimal.equalsreturnsfalseif they have a different scale so that "5.5" is not equal "5.50".- Parameters:
aObj1- first value. May benull.aObj2- second value. May benull.- Returns:
trueif they contain the same value
-
isEQ0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 0.
-
isNE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 0.
-
isLT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 0.
-
isLE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 0.
-
isGT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 0.
-
isGE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 0.
-
isEQ1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 1.
-
isNE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 1.
-
isLT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 1.
-
isLE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 1.
-
isGT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 1.
-
isGE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 1.
-
isEQ10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 10.
-
isNE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 10.
-
isLT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 10.
-
isLE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 10.
-
isGT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 10.
-
isGE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 10.
-
isEQ100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 100.
-
isNE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 100.
-
isLT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 100.
-
isLE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 100.
-
isGT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 100.
-
isGE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 100.
-
isEQ0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 0.
-
isNE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 0.
-
isLT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 0.
-
isLE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 0.
-
isGT0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 0.
-
isGE0
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 0.
-
isEQ1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 1.
-
isNE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 1.
-
isLT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 1.
-
isLE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 1.
-
isGT1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 1.
-
isGE1
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 1.
-
isEQ10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 10.
-
isNE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 10.
-
isLT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 10.
-
isLE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 10.
-
isGT10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 10.
-
isGE10
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 10.
-
isEQ100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is = 100.
-
isNE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is != 100.
-
isLT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is < 100.
-
isLE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≤ 100.
-
isGT100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is > 100.
-
isGE100
- Parameters:
aValue- Value to compare. May not benull.- Returns:
trueif the value is ≥ 100.
-
getWithoutTrailingZeroes
@CheckReturnValue public static @Nullable BigDecimal getWithoutTrailingZeroes(@Nullable String sValue) Get the passed String as a BigDecimal without any trailing zeroes.- Parameters:
sValue- The String to be used as a BigDecimal to be modified. May benull.- Returns:
nullif the input value isnull.
-
getWithoutTrailingZeroes
@CheckReturnValue public static @Nullable BigDecimal getWithoutTrailingZeroes(@Nullable BigDecimal aValue) Get the passed BigDecimal without any trailing zeroes. Examples:- new BigDecimal ("0.00000000") --> 0
- new BigDecimal ("10") --> 10
- new BigDecimal ("10.00000000") --> 10
- new BigDecimal ("10.1") --> 10.1
- new BigDecimal ("10.10000000") --> 10.1
- new BigDecimal ("10.345") --> 10.345
- new BigDecimal ("10.3450000000") --> 10.345
- Parameters:
aValue- The BigDecimal to be modified. May benull.- Returns:
nullif the input value isnull.
-
getFractionDigits
Get the number of effective fraction digits by the specified BigDecimal. Examples:- new BigDecimal ("10") --> 0
- new BigDecimal ("10.00000000") --> 0
- new BigDecimal ("10.1") --> 1
- new BigDecimal ("10.10000000") --> 1
- new BigDecimal ("10.345") --> 3
- new BigDecimal ("10.3450000000") --> 3
- Parameters:
aBD- The BigDecimal to check. May not benull.- Returns:
- The minimum number of fraction digits. Always ≥ 0.
-
addPercent
public static @NonNull BigDecimal addPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Add x% to base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- base + x% (
=aBase * (100 + perc) / 100). Nevernull.
-
addPercent
public static @NonNull BigDecimal addPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode) Add x% to base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- base + x% (
=aBase * (100 + perc) / 100). Nevernull.
-
subtractPercent
public static @NonNull BigDecimal subtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Subtract x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- base - x% (
=aBase * (100 - perc) / 100). Nevernull.
-
subtractPercent
public static @NonNull BigDecimal subtractPercent(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode) Subtract x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- base - x% (
=aBase * (100 - perc) / 100). Nevernull.
-
getPercentValue
public static @NonNull BigDecimal getPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage) Get x% from base- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.- Returns:
- x% from base (
=aBase * perc / 100). Nevernull.
-
getPercentValue
public static @NonNull BigDecimal getPercentValue(@NonNull BigDecimal aBase, @NonNull BigDecimal aPercentage, @Nonnegative int nScale, @NonNull RoundingMode eRoundingMode) Get x% from base with rounding etc.- Parameters:
aBase- Base value. May not benull.aPercentage- Percentage value (0-100). May not benull.nScale- Maximum scale to achieve.eRoundingMode- Rounding mode to used. May not benull.- Returns:
- x% from base (
=aBase * perc / 100). Nevernull.
-
toBigDecimal
Convert an int to aBigDecimal.- Parameters:
n- The int value.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigDecimal
Convert a long to aBigDecimal.- Parameters:
n- The long value.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigDecimal
Convert a float to aBigDecimal.- Parameters:
f- The float value.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigDecimal
Convert a double to aBigDecimal.- Parameters:
d- The double value.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigDecimal
Convert aNumberto aBigDecimal.- Parameters:
aNumber- The number to convert. May not benull.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigDecimal
Convert a String to aBigDecimal.- Parameters:
sNumber- The string representation of the number. May not benull.- Returns:
- The corresponding
BigDecimal. Nevernull.
-
toBigInteger
Convert an int to aBigInteger.- Parameters:
n- The int value.- Returns:
- The corresponding
BigInteger. Nevernull.
-
toBigInteger
Convert a long to aBigInteger.- Parameters:
n- The long value.- Returns:
- The corresponding
BigInteger. Nevernull.
-
toBigInteger
Convert aNumberto aBigInteger.- Parameters:
aNumber- The number to convert. May not benull.- Returns:
- The corresponding
BigInteger. Nevernull.
-
toBigInteger
Convert a String to aBigInteger.- Parameters:
sNumber- The string representation of the number. May not benull.- Returns:
- The corresponding
BigInteger. Nevernull.
-