Mathematical operations in ScriptCode
Mathematical operations in ScriptCode
It is recommended that all mathematical operations in scriptCode be performed through the BigDecimal.
API
big-decimal.interface.ts
big-decimal.interface.tsBigDecimal
interface BigDecimal {
valueOf(value: string | number | BigDecimal): BigDecimal
add(augend: string | number | BigDecimal): BigDecimal
subtract(subtrahend: string | number | BigDecimal): BigDecimal
multiply(multiplicand: string | number | BigDecimal): BigDecimal
divide(divisor: string | number | BigDecimal): BigDecimal
divide(divisor: string | number | BigDecimal, roundingMode: string): BigDecimal
divide(divisor: string | number | BigDecimal, scale: number): BigDecimal
divide(divisor: string | number | BigDecimal, scale: number, roundingMode: string): BigDecimal
setScale(newScale: number): BigDecimal
setScale(newScale: number, roudingMode: string): BigDecimal
equals(equated: string | number | BigDecimal): boolean
compareTo(compared: string | number | BigDecimal): int
}decimal-format.interface.ts
decimal-format.interface.tsinterface DecimalFormat {
of(pattern: string): DecimalFormat
of(pattern: string, symbols: DecimalFormatSymbols): DecimalFormat
format(value: BigDecimal): string
parse(value: string): BigDecimal
}rounding-mode.interface.ts
rounding-mode.interface.tsenum RoundingMode {
UP,
DOWN,
CEILING,
FLOOR,
HALF_UP,
HALF_DOWN,
HALF_EVEN
}decimal-format-symbols.interface.ts
decimal-format-symbols.interface.tsenum DecimalFormatSymbols {
PL,
EN,
ES,
SK,
CZ
}random.interface.ts
random.interface.tsinterface Random {
uuid(): string
}BigDecimal
Creating an object
var value = BigDecimal.valueOf("10.1000"); // RecommendedObject BigDecimal is immutable.
Operations such as add() do not change the value, they require assigning the result to a variable.
Mathematical operations
Addition
let value = BigDecimal.valueOf("1");
value = value.add("5"); // 1 + 5 = 6Subtraction
let value = BigDecimal.valueOf("100");
value = value.subtract("5"); // 100 - 5 = 95Division
let value = BigDecimal.valueOf("100");
value = value.divide("10"); // 100 / 10 = 10
value = value.divide("3", 5, RoundingMode.UP); // 10 / 3 = 3.33334Multiplication
let value = BigDecimal.valueOf("1");
value = value.multiply("20"); // 1 * 20 = 20Setting the scale
value = BigDecimal.valueOf("10.1000");
value = value.setScale(2); // = 10.10
value = value.setScale(1, RoundingMode.HALF_UP); // = 10.1Comparison and equality
value.equals("10"); // false
value.compareTo("5"); // 1DecimalFormat
Class for formatting decimal numbers in different languages. Documentation: DecimalFormat (Java)
Example
let decimalFormatPL = DecimalFormat.of("#.#", DecimalFormatSymbols.PL);
decimalFormatPL.format(BigDecimal.valueOf("34.567")); // 34,6Patterns
0
Digit placeholder
#
Digit placeholder (0 omitted)
,
Decimal separator
%
Multiplies by 100 and displays as a percent
E
Scientific notation
Rounding numbers (RoundingMode)
RoundingMode)HALF_UP
Rounds up if the decimal part ≥ 0.5
CEILING
Always up to the nearest integer
DOWN
Always down
FLOOR
Down to the nearest integer
HALF_DOWN
Down if < 0.5
HALF_EVEN
Banker's rounding (to the even number)
Preferred result scales
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/math/BigDecimal.html
Add
max(addend.scale(), augend.scale())
Subtract
max(minuend.scale(), subtrahend.scale())
Multiply
multiplier.scale() + multiplicand.scale()
Divide
dividend.scale() - divisor.scale()
Square root
radicand.scale()/2
Random
Generating UUIDs (version 4):
function callService(context) {
return [{ 'output': Random.uuid() }];
}Last updated
Was this helpful?
