Ramda-centric wrapper to decimal.js
npm install ramda-decimal
Use Decimal.js arbitrary precision maths in Ramda style. Immutable. Composable. Curry-able.
Drop-in replacements for most Ramda math functions.
const R = require('ramda);
const RD = require('ramda-decimal);
const annualTax = R.pipe(
R.pluck(income),
RD.sum,
RD.times(TAX_RATE),
);
const format = RD.fixed(2);
console.log(format(annualTax(months)));
Like Decimal.js, RD accepts Decimals, number primitives and strings that look like numbers. RD does this by calling new Decimal() on any input.
All RD functions return Decimals.
ZERO and ONE are defined as constants. Use them! They're immutable.
When there is a corresponding Ramda function, RD uses the same parameter order. This does mean that curried relation functions
are unintuitive (in my opinion) in the same way as Ramda:
const f = RD.gt(5);
return f(6); // false: gt(5,6)
Where there is no corresponding Ramda function, we have flipped the parameters when it seems useful for point-free coding:
const formatToPennies = RD.toFixed(2);
For brevity the documentation suggests that (for example)RD.add(a)(b) is equivalent to new Decimal(a).plus(b). The reality is
a bit better than that. RD.add(a) stores any Decimal it creates
for reuse. Hence in:
const addTen = RD.add(10);
const n110 = addTen(100);
const n210 = addTen(200);
... new Decimal(10) is only run once.
Returned values are just Decimal objects, so you are free to combine styles
if you want to:
const displayTotal = RD.sum(vals).fixed(2, Decimal.ROUND_HALF_UP);
Ramda-style curried functions wrapping the methods of Decimal.js
Decimal | number | stringA Decimal, number, or a string containing a number.
That is, any valid parameter to new Decimal(...)
Decimal \| number \| stringnew Decimal(...)Decimalnew Decimal(n).absoluteValue()Kind: static function
| Param | Type |
| --- | --- |
| n | DecimalLike |
Decimalnew Decimal(a).plus(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(n).ceil()Kind: static function
| Param | Type |
| --- | --- |
| n | DecimalLike |
booleannew Decimal(a).equals(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(a).dividedBy(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(b).dividedBy(a)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(n).floor()Kind: static function
| Param | Type |
| --- | --- |
| n | DecimalLike |
booleannew Decimal(a).gt(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
booleannew Decimal(a).gte(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
booleannew Decimal(a).lt(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
booleannew Decimal(a).lte(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(a).times(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(a).negated()Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
Decimalnew Decimal(a).round()Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
Decimalnew Decimal(a).minus(b)Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
| b | DecimalLike |
Decimalnew Decimal(num).toFixed(roundingMode)See Decimal.js docs for ROUNDING_MODE constants
Kind: static function
| Param | Type |
| --- | --- |
| roundingMode | num |
| num | DecimalLike |
Numbernew Decimal(a).toNumber()Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
Decimalnew Decimal(value).modulo(modulus)Kind: static function
| Param | Type |
| --- | --- |
| modulus | DecimalLike |
| value | DecimalLike |
Decimalnew Decimal(value).toPower(modulus)Kind: static function
| Param | Type |
| --- | --- |
| power | DecimalLike |
| value | DecimalLike |
booleannew Decimal(a).isPositive()Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
booleannew Decimal(a).isNegative()Kind: static function
| Param | Type |
| --- | --- |
| a | DecimalLike |
DecimalKind: static function
Returns: Decimal - a minus 1
| Param | Type |
| --- | --- |
| a | DecimalLike |
DecimalKind: static function
Returns: Decimal - a plus 1
| Param | Type |
| --- | --- |
| a | DecimalLike |
DecimalKind: static function
Returns: Decimal - The sum of the values in a
| Param | Type |
| --- | --- |
| a | Array.<DecimalLike> |
DecimalKind: static function
Returns: Decimal - The product of the values in a
| Param | Type |
| --- | --- |
| a | Array.<DecimalLike> |
DecimalKind: static function
Returns: Decimal - The highest of a or b
| Param | Type |
| --- | --- |
| a | Array.<DecimalLike> |
| b | Array.<DecimalLike> |
DecimalKind: static function
Returns: Decimal - The lowest of a or b
| Param | Type |
| --- | --- |
| a | Array.<DecimalLike> |
| b | Array.<DecimalLike> |
------
Created by Wealth Wizards Software Engineering - http://wealthwizards.com