Class BigDecimalMath
BigDecimal that is not covered by its built-in methods.- Since:
- 30.0
- Author:
- Louis Wasserman
-
Method Summary
Modifier and TypeMethodDescriptionstatic doubleroundToDouble(BigDecimal x, RoundingMode mode) Returnsx, rounded to adoublewith the specified rounding mode.static BigIntegertoBigInteger(BigDecimal x, int maxIntegerDigits) Returnsxconverted to aBigInteger, ensuring that the magnitude ofxdoes not exceedmaxIntegerDigitsbefore performing conversion.static BigIntegertoBigIntegerExact(BigDecimal x, int maxIntegerDigits) Returnsxconverted to aBigInteger, checking thatxhas no fractional part and ensuring that the magnitude ofxdoes not exceedmaxIntegerDigitsbefore performing conversion.
-
Method Details
-
toBigInteger
Returnsxconverted to aBigInteger, ensuring that the magnitude ofxdoes not exceedmaxIntegerDigitsbefore performing conversion.Converting a
BigDecimalwith a very large negative scale to aBigIntegerrequires materializing the unscaled value multiplied by10^-scale. Whenxis parsed from uncontrolled string or JSON inputs (such as"1e123456789"), directly callingBigDecimal.toBigInteger()orBigDecimal.toBigIntegerExact()can exhaust CPU and memory. This method safely verifies that the number of decimal digits required for the integer representation fits withinmaxIntegerDigitsbefore conversion.Choosing a reasonable value for
maxIntegerDigitsdepends on the range of expected valid values for your input. A value of 100 is generally sufficient for most common use cases and provides a large safety margin against resource exhaustion from malicious inputs.- Parameters:
x- theBigDecimalto convert to aBigIntegermaxIntegerDigits- the maximum allowable number of digits in the integer part (for values where|x| < 1, the integer part is considered to have 0 digits)- Returns:
xconverted to aBigInteger- Throws:
IllegalArgumentException- ifmaxIntegerDigits < 0ArithmeticException- if the integer part ofxrequires more thanmaxIntegerDigitsdigits- Since:
- NEXT
-
toBigIntegerExact
Returnsxconverted to aBigInteger, checking thatxhas no fractional part and ensuring that the magnitude ofxdoes not exceedmaxIntegerDigitsbefore performing conversion.Converting a
BigDecimalwith a very large negative scale to aBigIntegerrequires materializing the unscaled value multiplied by10^-scale. Whenxis parsed from uncontrolled string or JSON inputs (such as"1e123456789"), directly callingBigDecimal.toBigInteger()orBigDecimal.toBigIntegerExact()can exhaust CPU and memory. This method safely verifies that the number of decimal digits required for the integer representation fits withinmaxIntegerDigitsbefore conversion.Choosing a reasonable value for
maxIntegerDigitsdepends on the range of expected valid values for your input. A value of 100 is generally sufficient for most common use cases and provides a large safety margin against resource exhaustion from malicious inputs.- Parameters:
x- theBigDecimalto convert to aBigIntegermaxIntegerDigits- the maximum allowable number of digits in the integer part (for values where|x| < 1, the integer part is considered to have 0 digits)- Returns:
xconverted to aBigInteger- Throws:
IllegalArgumentException- ifmaxIntegerDigits < 0ArithmeticException- ifxhas a nonzero fractional part or if the integer part ofxrequires more thanmaxIntegerDigitsdigits- Since:
- NEXT
-
roundToDouble
Returnsx, rounded to adoublewith the specified rounding mode. Ifxis precisely representable as adouble, itsdoublevalue will be returned; otherwise, the rounding will choose between the two nearest representable values withmode.For the case of
RoundingMode.HALF_DOWN,HALF_UP, andHALF_EVEN, infinitedoublevalues are considered infinitely far away. For example, 2^2000 is not representable as a double, butroundToDouble(BigDecimal.valueOf(2).pow(2000), HALF_UP)will returnDouble.MAX_VALUE, notDouble.POSITIVE_INFINITY.For the case of
RoundingMode.HALF_EVEN, this implementation uses the IEEE 754 default rounding mode: if the two nearest representable values are equally near, the one with the least significant bit zero is chosen. (In such cases, both of the nearest representable values are even integers; this method returns the one that is a multiple of a greater power of two.)- Throws:
ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not precisely representable as adouble- Since:
- 30.0
-