BigDecimal
Constructors and methods of BigDecimal for exact decimal arithmetic, and of Money for formatting amounts as currency, in server scripts.
BigDecimal is the global for exact decimal arithmetic in server scripts, and Money formats amounts as currency. Use them for money and any value where JavaScript numbers lose precision: in JavaScript 0.1 * 3 is 0.30000000000000004. A Money field returns its value as a BigDecimal.
Get an instance
Call BigDecimal as a function. Pass a string where you can, because a string keeps exactly the digits you write.
let price = BigDecimal("19.99");
let rate = BigDecimal(0.1);
let copy = BigDecimal(price);
| Method | Returns | Description | Since |
|---|---|---|---|
BigDecimal(text) |
BigDecimal | Parses a decimal string, for example "19.99". |
1.11 |
BigDecimal(text, pattern) |
BigDecimal | Parses text with a Java DecimalFormat pattern, using the separators of the locale set by BigDecimal.withLang(). |
1.11 |
BigDecimal(number) |
BigDecimal | Converts a JavaScript number. | 1.11 |
BigDecimal(bigDecimal) |
BigDecimal | A copy of another BigDecimal. |
1.11 |
BigDecimal.withLang(locale) |
BigDecimal global | Sets the locale for later parsing and formatting, for example "fr-FR". Strings are then parsed with that locale’s separators. |
1.11 |
BigDecimal.withRounding(mode) |
BigDecimal global | Sets the rounding mode for the rest of the script. The default is HALF_UP. |
1.11 |
BigDecimal.withPrecision(digits) |
BigDecimal global | Sets the number of significant digits for the rest of the script. The default, 0, means unlimited. | 1.11 |
BigDecimal.getRounding() |
String | The current rounding mode. | 1.11 |
BigDecimal.getPrecision() |
Number | The current precision. | 1.11 |
BigDecimal.roundingModes |
Array | The rounding mode names: UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN and UNNECESSARY. |
1.11 |
With unlimited precision, a division that never ends, such as 1 divided by 3, raises an error. Call BigDecimal.withPrecision() before such a division.
Methods
Every method that returns a BigDecimal returns a new one, so calls chain and the original never changes. In 1.11 an operand must be a BigDecimal.
Arithmetic
| Method | Returns | Description | Since |
|---|---|---|---|
add(value) |
BigDecimal | This plus value. |
1.11 |
subtract(value) |
BigDecimal | This minus value. |
1.11 |
multiply(value) |
BigDecimal | This times value. |
1.11 |
divide(value) |
BigDecimal | This divided by value, at the script’s precision and rounding mode. |
1.11 |
remainder(value) |
BigDecimal | The remainder after dividing by value. |
1.11 |
divideToIntegralValue(value) |
BigDecimal | The whole-number part of dividing by value. |
1.11 |
divideAndRemainder(value) |
Array | The whole-number part and the remainder, as a two-item array. | 1.11 |
pow(n) |
BigDecimal | This to the power of the whole number n. |
1.11 |
sqrt() |
BigDecimal | The square root, at the script’s precision. | 1.11 |
abs() |
BigDecimal | The absolute value. | 1.11 |
negate() |
BigDecimal | The value with its sign changed. | 1.11 |
plus() |
BigDecimal | The value itself, rounded to the script’s precision. | 1.11 |
min(value), max(value) |
BigDecimal | The smaller or the larger of the two. | 1.11 |
movePointLeft(n), movePointRight(n) |
BigDecimal | Moves the decimal point n places. |
1.11 |
scaleByPowerOfTen(n) |
BigDecimal | This times 10 to the power of n. |
1.11 |
Rounding and scale
| Method | Returns | Description | Since |
|---|---|---|---|
round() |
BigDecimal | Rounds to the script’s precision. With the default unlimited precision the value is unchanged. | 1.11 |
round(mode) |
BigDecimal | Rounds to the script’s precision with the given rounding mode. | 1.11 |
round(digits, mode) |
BigDecimal | Rounds to digits significant digits with the given rounding mode. |
1.11 |
setScale(places, mode) |
BigDecimal | Sets the number of decimal places. In 1.11 the script’s rounding mode applies, not mode. |
1.11 |
setPrecision(digits) |
BigDecimal | Rounds to digits significant digits with the script’s rounding mode. |
1.11 |
stripTrailingZeros() |
BigDecimal | Removes zeros at the end of the decimal part. | 1.11 |
scale() |
Number | The number of decimal places. | 1.11 |
precision() |
Number | The number of significant digits. | 1.11 |
ulp() |
BigDecimal | The size of one unit in the last decimal place. | 1.11 |
unscaledValue() |
Number | The digits without the decimal point, as a whole number. | 1.11 |
Compare and convert
| Method | Returns | Description | Since |
|---|---|---|---|
compareTo(value) |
Number | -1, 0 or 1 when this is less than, equal to or greater than value. 2.0 and 2.00 compare as equal. |
1.11 |
signum() |
Number | -1, 0 or 1 for a negative, zero or positive value. | 1.11 |
toDouble() |
Number | The value as a JavaScript number, which can lose precision. | 1.11 |
toLong() |
Number | The whole-number part. | 1.11 |
toBigInteger() |
Number | The whole-number part, without a size limit. | 1.11 |
toPlainString() |
String | The value as digits, without an exponent. toString() gives the same. |
1.11 |
toEngineeringString() |
String | The value in engineering notation when an exponent is needed. | 1.11 |
format() |
String | The value with the locale’s grouping and decimal separators, up to three decimal places. | 1.11 |
format(pattern) |
String | The value formatted with a Java DecimalFormat pattern, such as "#,##0.00", and the locale’s separators. |
1.11 |
withLang(locale) |
BigDecimal | A copy that formats with another locale. | 1.11 |
Money
Money formats a BigDecimal as a currency amount. Currency codes are three-letter ISO 4217 codes. Without a currency, Money uses the system.currency application property. Without a locale, it uses the current user’s locale.
| Method | Returns | Description | Since |
|---|---|---|---|
Money.format(amount) |
String | Formats in the default currency and the user’s locale. | 1.11 |
Money.format(amount, currency) |
String | Formats in currency, for example "EUR". |
1.11 |
Money.format(amount, currency, locale) |
String | Formats in currency for locale, for example "de-DE". |
1.11 |
Money.format(amount, currency, useSymbol, locale) |
String | As above. With useSymbol true, USD, AUD, NZD and CAD show $ and EUR shows € whatever the locale. |
1.11 |
Money.getFieldCurrencyCode(field) |
String | The currency code of a Money field, or the default currency when the field sets none. Returns null for other field types. | 1.11 |
Examples
Multiply exactly
Servicely Script
BigDecimal("0.1")
.multiply(BigDecimal("3"))
.toPlainString();
Response
"0.3"
Format for a locale
Servicely Script
BigDecimal("0.1").withLang("fr-FR").format();
Response
"0,1"
Format a Money field as currency
// <MoneyField> is a Money field on the current record
let amount = current.<MoneyField>();
let currency = Money.getFieldCurrencyCode(current.<MoneyField>);
Money.format(amount, currency, "de-DE");