Class MathUtil
java.lang.Object
com.ssgllc.fish.service.util.registered.MathUtil
Numeric helpers for calculated fields, validations and scripts.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic doubleceil(double x) Rounds a value up to the nearest whole number.createIntRange(Integer minValue, Integer maxValue) Builds the inclusive list of integers fromminValuetomaxValue.static doublefloor(double x) Rounds a value down to the nearest whole number.static BigDecimalgetBigDecimalFromObject(Object value) Coerces any numeric-looking value to aBigDecimal.static doublerandom()Returns a pseudo-random value between 0.0 inclusive and 1.0 exclusive.static doubleround(double x, int scale) Rounds a value to the given number of decimal places, half-up.static BigDecimaltoBigDecimal(int value) Converts an int to aBigDecimal.static inttoIntExact(long value) Narrows a long to an int, refusing to do so silently when the value will not fit.
-
Constructor Details
-
MathUtil
public MathUtil()
-
-
Method Details
-
round
public static double round(double x, int scale) Rounds a value to the given number of decimal places, half-up.- Parameters:
x- the value to roundscale- number of decimal places to keep; may be negative to round to tens, hundreds and so on- Returns:
- the rounded value
Groovy example:
return mathUtil.round(3.14159, 2)
Returns:
3.14
SpEL example:
#round(#amount * 1.0825, 2)
Returns:
the amount plus tax, to the cent
-
ceil
public static double ceil(double x) Rounds a value up to the nearest whole number.- Parameters:
x- the value to round up- Returns:
- the smallest whole number that is greater than or equal to
x
Groovy example:
return mathUtil.ceil(4.1)
Returns:
5.0
SpEL example:
#ceil(#doseCount / 3.0)
Returns:
the number of full boxes needed to cover the doses
-
floor
public static double floor(double x) Rounds a value down to the nearest whole number.- Parameters:
x- the value to round down- Returns:
- the largest whole number that is less than or equal to
x
Groovy example:
return mathUtil.floor(4.9)
Returns:
4.0
SpEL example:
#floor(#ageInMonths / 12.0)
Returns:
whole years completed
-
random
public static double random()Returns a pseudo-random value between 0.0 inclusive and 1.0 exclusive. Not suitable for anything security-related; use it for sampling and test data only.- Returns:
- a pseudo-random value in
[0.0, 1.0)
Groovy example:
return mathUtil.random() < 0.1
Returns:
true for roughly one call in ten
SpEL example:
#random()
Returns:
0.5488135039273248
-
toIntExact
public static int toIntExact(long value) Narrows a long to an int, refusing to do so silently when the value will not fit. Prefer this over a cast where the value comes from a count or an id, so an overflow surfaces as an error rather than as a negative number.- Parameters:
value- the value to narrow- Returns:
- the same value as an int
- Throws:
ArithmeticException- ifvaluedoes not fit in an int
Groovy example:
return mathUtil.toIntExact(42L)
Returns:
42
SpEL example:
#toIntExact(#recordCount)
Returns:
the count as an int
-
toBigDecimal
Converts an int to aBigDecimal. Useful where a calculated field is typed as a decimal and the expression produced a whole number.- Parameters:
value- the value to convert- Returns:
- the value as a
BigDecimal
Groovy example:
return mathUtil.toBigDecimal(7)
Returns:
7
SpEL example:
#toBigDecimal(#quantity)
Returns:
the quantity as a decimal
-
createIntRange
Builds the inclusive list of integers fromminValuetomaxValue. Returns an empty list rather than failing when either bound is null or the range is inverted, so it is safe to drive from optional fields.- Parameters:
minValue- lower bound, included in the resultmaxValue- upper bound, included in the result- Returns:
- the integers in the range, or an empty list if either bound is null or
maxValue < minValue
Groovy example:
return mathUtil.createIntRange(1, 5)
Returns:
[1, 2, 3, 4, 5]
SpEL example:
#createIntRange(1, #doseSeriesLength)
Returns:
the dose numbers in the series
-
getBigDecimalFromObject
Coerces any numeric-looking value to aBigDecimal. Accepts the numeric wrapper types, a numeric string, and a single digit character. Use it where a value arrives untyped - from an import row, a form field or a map - and a decimal is needed. A value that cannot represent a number is an error rather than a null, so a bad import row fails loudly instead of silently becoming zero.- Parameters:
value- the value to convert; may benull- Returns:
- the value as a
BigDecimal, ornullifvalueis null - Throws:
IllegalArgumentException- if the value is a blank string, a non-numeric string, a non-digit character, or an object whosetoString()is not numeric
Groovy example:
return mathUtil.getBigDecimalFromObject(row.casetivityExtraFields['amount'])
Returns:
the amount as a decimal, whatever type the import produced
SpEL example:
#getBigDecimalFromObject(#value)
Returns:
12.50
-