Class NumberUtils
- Since:
- 1.42
- Version:
- 1.10
- Author:
- matt
-
Method Summary
Modifier and TypeMethodDescriptionstatic BigDecimalbigDecimalForNumber(Number value) Get aBigDecimalfor a number.static BigIntegerGet an integer for aBitSet.static BigIntegerbigIntegerForNumber(Number value) Get aBigIntegerfor a number.static BitSetbitSetForBigInteger(BigInteger value) Get aBitSetfor an integer.static NumberRound positive numbers away from zero and negative numbers towards zero, to the nearest integer multiple of a specific significance.static intcrc16(byte[] data, int offset, int length) Calculate the CRC-16 checksum value from a set of data.static BigDecimal[]decimalArray(String... nums) Get an array ofBigDecimalinstances from string values.static NumberDivide twoNumberinstances using a specific implementation of Number.static DoubleAttempt to convert aBigDecimalexactly to adouble.static NumberRound a number towards zero to the nearest integer multiple of a specific significance.static FloatAttempt to convert aBigDecimalexactly to afloat.static NumberRound positive numbers towards zero and negative numbers away from zero, to the nearest integer multiple of a specific significance.static BigIntegerfractionalPartScaledToInteger(BigDecimal decimal, int scale) Get the fractional part of aBigDecimalas aBigInteger, scaled by some power of ten.static BigIntegerfractionalPartToInteger(BigDecimal decimal) Get the fractional part of aBigDecimalas aBigInteger.static BigIntegerfractionalPartToInteger(BigDecimal decimal, int maxScale) Get the fractional part of aBigDecimalas aBigIntegerwith a maximum scale.static intgetAndIncrementWithWrap(AtomicInteger n, int restart) static longgetAndIncrementWithWrap(AtomicLong n, long restart) static StringhumanReadableCount(long count) Format a count in a "human readable" format, with progressively larger SI units.static NumberReturn the maximum between two number values.static NumbermaximumDecimalScale(Number value, int maxDecimalScale) Apply a maximum decimal scale to a number value.static NumberReturn the minimum between two number values.static NumberRound a number to the nearest integer multiple of a specific significance.static Numbermround(Number n, Number significance, RoundingMode mode) Round a number to the nearest integer multiple of a specific significance using a specific rounding mode.static Numbermultiplied(Number value, BigDecimal multiple) Apply a multiplier to a number value.static NumberMultiply two Number instances.static NumberNarrow a number to the smallest possible number type that can exactly represent the given number.static Numbernarrow(BigDecimal n, int minBytePower) Narrow aBigDecimalto the smallest possible number type that can exactly represent the given number.static Numbernarrow(BigInteger n, int minBytePower) Narrow aBigIntegerto the smallest possible number type that can exactly represent the given number.static Numberoffset(Number value, BigDecimal offset) Apply an offset to a number value.static NumberparseNumber(String numberString, Class<? extends Number> numberType) Parse a String into a Number of a specific type.static NumberRound a number to a maximum number of decimal digits using theRoundingMode.HALF_UPmode.static Numberround(Number n, Number digits, RoundingMode mode) Round a number to a maximum number of decimal digits.static NumberRound a number towards zero to a maximum number of decimal digits.static NumberRound a number away from zero to a maximum number of decimal digits.static BigDecimalScale a number by a power of 10.static NumberSubtract two Number instances.static shortunsigned(byte data) Convert a signed byte into an unsigned short value.static short[]unsigned(byte[] data) Convert signed bytes into unsigned short values.static NumberunsignedNumber(Number value) Convert a signed whole number into an unsigned equivalent.static NumberRound a number towards zero to the nearest integer multiple of a specific significance.static BigIntegerwholePartToInteger(BigDecimal decimal) Get the whole part of aBigDecimalas aBigInteger.
-
Method Details
-
unsigned
public static short unsigned(byte data) Convert a signed byte into an unsigned short value.The returned short will have a value between 0 and 255.
- Parameters:
data- the byte- Returns:
- the unsigned value
-
unsigned
public static short[] unsigned(byte[] data) Convert signed bytes into unsigned short values.The returned shorts will have values between 0 and 255.
- Parameters:
data- the bytes- Returns:
- the unsigned values, or null if
datais null
-
unsignedNumber
Convert a signed whole number into an unsigned equivalent.This method attempts to return the next-largest data type for the unsigned conversion, e.g. an unsigned int will be returned for a signed short.
- Parameters:
value- the signed whole number to convert- Returns:
- the unsigned value, or null if
valueis null
-
bigDecimalForNumber
Get aBigDecimalfor a number.If
valueis already aBigDecimalit will be returned directly. Otherwise a newBigDecimalinstance will be created out ofvalue.- Parameters:
value- the number to get aBigDecimalversion of- Returns:
- the
BigDecimalversion ofvalue, or null ifvalueis null
-
bigIntegerForNumber
Get aBigIntegerfor a number.If
valueis already aBigIntegerit will be returned directly. Otherwise a newBigIntegerinstance will be created out ofvalue.- Parameters:
value- the number to get aBigIntegerversion of- Returns:
- the
BigIntegerversion ofvalue, or null ifvalueis null - Since:
- 1.8
-
decimalArray
Get an array ofBigDecimalinstances from string values.- Parameters:
nums- the string numbers- Returns:
- the array
- Throws:
NumberFormatException- if any number is not a valid representation of aBigDecimal- Since:
- 1.6
-
crc16
public static int crc16(byte[] data, int offset, int length) Calculate the CRC-16 checksum value from a set of data.Adapted from https://introcs.cs.princeton.edu/java/61data/CRC16.java.
- Parameters:
data- the data to checksumoffset- the offset within the data to startlength- the length of data to checksum- Returns:
- the checksum value
- Since:
- 1.1
-
wholePartToInteger
Get the whole part of aBigDecimalas aBigInteger.If whole portion of the decimal is returned without any rounding from the fractional part of the decimal.
- Parameters:
decimal- the decimal- Returns:
- the whole part as an integer, or zero if
decimalis null - Since:
- 1.2
-
fractionalPartToInteger
Get the fractional part of aBigDecimalas aBigInteger.- Parameters:
decimal- the decimal- Returns:
- the fractional part as an integer, or zero if
decimalis null - Since:
- 1.2
-
fractionalPartToInteger
Get the fractional part of aBigDecimalas aBigIntegerwith a maximum scale.If the fractional part must be rounded, the
RoundingMode.FLOORmethod (when positive) orRoundingMode.CEILING(when negative) will be used to truncate the value to keep it within the desired scale.- Parameters:
decimal- the decimalmaxScale- the maximum power-of-10 scale- Returns:
- the fractional part as an integer, or zero if
decimalis null - Since:
- 1.2
-
fractionalPartScaledToInteger
Get the fractional part of aBigDecimalas aBigInteger, scaled by some power of ten.For example, to convert the fractional part to "nano" scale, pass in 9 for the scale.
- Parameters:
decimal- the decimal to get the scaled fractional part fromscale- the power of 10 to scale by; a negative value shifts the decimal point left this many places; a positive value shifts the decimal point right this many places- Returns:
- the fractional part as an integer, or zero if
decimalis null - Since:
- 1.2
-
scaled
Scale a number by a power of 10.- Parameters:
num- the number to scalescale- the power of 10 to scale by; a negative value shifts the decimal point left this many places; a positive value shifts the decimal point right this many places- Returns:
- the scaled value
- Since:
- 1.2
-
maximumDecimalScale
Apply a maximum decimal scale to a number value.- Parameters:
value- the number to apply the maximum scale tomaxDecimalScale- the maximum scale, or -1 for no maximum- Returns:
- the value, possibly rounded to
decimalScale - Since:
- 1.3
-
multiplied
Apply a multiplier to a number value.- Parameters:
value- the source numbermultiple- the value to multiplyvalueby; if null thenvalueis returned unchanged- Returns:
- the value, or null if
valueis null - Since:
- 1.3
-
offset
Apply an offset to a number value.- Parameters:
value- the number to apply the offset tooffset- the value to add tovalue; if null thenvalueis returned unchanged- Returns:
- the value, or null if
valueis null - Since:
- 1.3
-
bigIntegerForBitSet
Get an integer for aBitSet.- Parameters:
bs- the bit set to convert to an integer representation- Returns:
- the integer, never null
- Since:
- 1.4
-
bitSetForBigInteger
Get aBitSetfor an integer.- Parameters:
value- the integer to convert to aBitSet- Returns:
- a
BitSetwith all set bits ofvalueset; never null - Throws:
IllegalArgumentException- ifvalueis negative
-
getAndIncrementWithWrap
- Parameters:
n- the atomic numberrestart- the value to wrap around to ifInteger.MAX_VALUEis returned- Returns:
- the incremented value, possibly wrapped around
- Since:
- 1.7
-
getAndIncrementWithWrap
- Parameters:
n- the atomic numberrestart- the value to wrap around to ifLong.MAX_VALUEis returned- Returns:
- the incremented value, possibly wrapped around
- Since:
- 1.7
-
parseNumber
Parse a String into a Number of a specific type.- Parameters:
numberString- the String to parsenumberType- the type of Number to return- Returns:
- the new Number instance
- Since:
- 1.8
-
divide
Divide twoNumberinstances using a specific implementation of Number.Really the
numberTypeargument should be considered aClass<? extends Number>but to simplify calling this method any Class is allowed.- Parameters:
dividend- the dividend valuedivisor- the divisor valuenumberType- the type of Number to treat the dividend and divisor as- Returns:
- a Number instance of type
numberType - Since:
- 1.8
-
subtract
Subtract two Number instances.The returned Number will be an instance of the
startclass.- Parameters:
start- the starting number to subtract fromoffset- the amount to subtract- Returns:
- a Number instance of the same type as
start - Since:
- 1.8
-
multiply
Multiply two Number instances.The returned Number will be an instance of the
aclass.- Parameters:
a- first numberb- second number- Returns:
- a Number instance of the same type as
a - Since:
- 1.8
-
humanReadableCount
Format a count in a "human readable" format, with progressively larger SI units.For example this method will return 7.1 MB for input of 7077888.
Adapted from @aioobe on Stack Overflow
- Parameters:
count- the count to format- Returns:
- the count formatted as a string
- Since:
- 1.9
-
min
Return the minimum between two number values.- Parameters:
n1- the first numbern2- the second number- Returns:
- the minimum number, or null if both arguments are null
- Since:
- 1.10
-
max
Return the maximum between two number values.- Parameters:
n1- the first numbern2- the second number- Returns:
- the maximum number, or null if both arguments are null
- Since:
- 1.10
-
down
Round a number towards zero to the nearest integer multiple of a specific significance.This method rounds using the
RoundingMode.DOWNmode.- Parameters:
n- the number to roundsignificance- the multiple factor to round to- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
- See Also:
-
up
Round a number towards zero to the nearest integer multiple of a specific significance.This method rounds using the
RoundingMode.UPmode.- Parameters:
n- the number to roundsignificance- the multiple factor to round to- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
- See Also:
-
floor
Round positive numbers towards zero and negative numbers away from zero, to the nearest integer multiple of a specific significance.This method rounds using the
RoundingMode.FLOORmode.- Parameters:
n- the number to roundsignificance- the multiple factor to round to- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
- See Also:
-
ceil
Round positive numbers away from zero and negative numbers towards zero, to the nearest integer multiple of a specific significance.This method rounds using the
RoundingMode.CEILINGmode.- Parameters:
n- the number to roundsignificance- the multiple factor to round to- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
- See Also:
-
mround
Round a number to the nearest integer multiple of a specific significance.This method rounds using the
RoundingMode.HALF_UPmode.- Parameters:
n- the number to roundsignificance- the multiple factor to round to- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
- See Also:
-
mround
Round a number to the nearest integer multiple of a specific significance using a specific rounding mode.This method supports rounding like supported by common spreadsheet application formulas
CEILING,FLOOR, andMROUND, which accept a significance factor to round to.- Parameters:
n- the number to roundsignificance- the multiple factor to round tomode- the rounding mode to use- Returns:
- the rounded number as a
BigDecimal, or null ifnorsignificanceare null - Since:
- 1.10
-
round
Round a number to a maximum number of decimal digits using theRoundingMode.HALF_UPmode.- Parameters:
n- the number to rounddigits- the maximum number of decimal digits- Returns:
- the rounded number as a
BigDecimal, or null ifnordigitsis null - Since:
- 1.10
- See Also:
-
roundUp
Round a number away from zero to a maximum number of decimal digits.- Parameters:
n- the number to rounddigits- the maximum number of decimal digits- Returns:
- the rounded number as a
BigDecimal, or null ifnordigitsis null - Since:
- 1.10
- See Also:
-
roundDown
Round a number towards zero to a maximum number of decimal digits.- Parameters:
n- the number to rounddigits- the maximum number of decimal digits- Returns:
- the rounded number as a
BigDecimal, or null ifnordigitsis null - Since:
- 1.10
- See Also:
-
round
Round a number to a maximum number of decimal digits.- Parameters:
n- the number to rounddigits- the maximum number of decimal digitsmode- the rounding mode- Returns:
- the rounded number as a
BigDecimal, or null ifnordigitsis null - Since:
- 1.10
-
narrow
Narrow a number to the smallest possible number type that can exactly represent the given number.If
ncannot be narrowed thennis returned. -
narrow
Narrow aBigIntegerto the smallest possible number type that can exactly represent the given number.If
ncannot be narrowed thennis returned. -
narrow
Narrow aBigDecimalto the smallest possible number type that can exactly represent the given number.If
ncannot be narrowed thennis returned. -
floatValueExact
Attempt to convert aBigDecimalexactly to afloat.- Parameters:
n- the number to convert- Returns:
- the converted value, or null if
nis null - Throws:
ArithmeticException- if an exact conversion cannot be done- Since:
- 1.10
-
doubleValueExact
Attempt to convert aBigDecimalexactly to adouble.- Parameters:
n- the number to convert- Returns:
- the converted value, or null if
nis null - Throws:
ArithmeticException- if an exact conversion cannot be done- Since:
- 1.10
-