| Statements | Branches | Functions | Lines |
|---|---|---|---|
npm install doubloonsImport types:
import { Doubloon } from 'doubloons';
import { USD } from 'doubloons/currencies';Create a Doubloon with a string or a Decimal (from Decimal.js) with the intended currency:
const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, new Decimal('10.50'));
console.log(x.add(y));Adding and subtracting between Doubloons is only supported with two Doubloons of the same currency:
const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, '5.05');
console.log(x.add(y));
console.log(x.subtract(y));Multiplying and dividing between Doubloons is forbidden, you can only multiply or divide by a scalar int (number with no decimals) or an instance of Decimal from Decimal.js.
const x = new Doubloon<USD>(USD, '10.00');
const y = new Doubloon<USD>(USD, '5.05');
console.log(x.mul(y)); // not allowed
console.log(x.mul(5)); // OK
console.log(x.mul(5.15)); // Not OK
console.log(x.mul(new Decimal(5.15)); // OKSimilarly, boolean comparisons require two Doubloons of the same kind.
To get a value out of a Doubloon, you can use .str() to get a string. This will be a properly-quantized decimal number for the given currency, without the currency symbol.