Skip to main content

NumericUtil

NumericUtil provides utility methods for numeric rounding and locale-aware number formatting and parsing. Use it in scripts where you need precise control over how decimal numbers are rounded or how number strings are converted to/from numeric values with custom separators.


Methods

round(value: Double, places: Integer): Double

Rounds a number to the specified number of decimal places using HALF_UP rounding (standard mathematical rounding — 0.5 rounds up).

ParameterTypeDescription
valueDoubleThe number to round
placesIntegerNumber of decimal places to round to

Returns: The rounded value as a Double.

NumericUtil.round(3.14159, 2) // → 3.14
NumericUtil.round(3.145, 2) // → 3.15
NumericUtil.round(3.145, 0) // → 3.0

roundUp(value: Double, places: Integer): Double

Rounds a number up (away from zero) to the specified number of decimal places, regardless of the next digit.

ParameterTypeDescription
valueDoubleThe number to round
placesIntegerNumber of decimal places

Returns: The rounded-up value.

NumericUtil.roundUp(3.141, 2) // → 3.15
NumericUtil.roundUp(3.100, 2) // → 3.10
NumericUtil.roundUp(-3.141, 2) // → -3.14 (away from zero = less negative)

roundDown(value: Double, places: Integer): Double

Rounds a number down (toward zero) to the specified number of decimal places, regardless of the next digit.

ParameterTypeDescription
valueDoubleThe number to round
placesIntegerNumber of decimal places

Returns: The rounded-down value.

NumericUtil.roundDown(3.149, 2) // → 3.14
NumericUtil.roundDown(3.150, 2) // → 3.15
NumericUtil.roundDown(-3.149, 2) // → -3.14 (toward zero = less negative)

parseCustomDouble(value: String, decimalSeparator: String, thousandSeparator: String): Double

Parses a number string using custom decimal and thousand separators. Use this when your ERP or data source uses a non-standard number format (for example, European format with , as the decimal separator).

ParameterTypeDescription
valueStringThe number string to parse
decimalSeparatorStringSingle-character string used as the decimal separator in value
thousandSeparatorStringSingle-character string used as the thousand separator in value. Pass an empty string "" if there is no thousand separator.

Returns: The parsed value as a Double.

Throws: A parse exception if the value is empty, or if either separator is null or longer than one character.

// European format: "1.000,50" → 1000.5
NumericUtil.parseCustomDouble("1.000,50", ",", ".")

// US format: "1,000.50" → 1000.5
NumericUtil.parseCustomDouble("1,000.50", ".", ",")

// No thousand separator: "1000,50" → 1000.5
NumericUtil.parseCustomDouble("1000,50", ",", "")

formatCustomDouble(value: Double, decimalSeparator: String, thousandSeparator: String): String

Formats a numeric value as a string using custom decimal and thousand separators. Use this when you need to produce output in a specific regional number format.

ParameterTypeDescription
valueDoubleThe number to format
decimalSeparatorStringSingle-character string to use as the decimal separator
thousandSeparatorStringSingle-character string to use as the thousand separator. Pass "" for no grouping.

Returns: The formatted number as a String.

// Format as European: 1000.5 → "1.000,50"
NumericUtil.formatCustomDouble(1000.5, ",", ".")

// Format as US: 1000.5 → "1,000.50"
NumericUtil.formatCustomDouble(1000.5, ".", ",")

Common Use Cases

Round currency values to 2 decimal places:

var total = 123.456789;
var rounded = NumericUtil.round(total, 2); // → 123.46

Parse an ERP amount string in European format:

var erpAmount = properties.get("AMOUNT_STRING"); // e.g., "1.234,56"
var numericAmount = NumericUtil.parseCustomDouble(erpAmount, ",", ".");

Format a calculated value for output:

var result = calculateTotal();
var formatted = NumericUtil.formatCustomDouble(result, ",", ".");
properties.set("FORMATTED_TOTAL", formatted);