Mojaloop Number Library implementation that handles decimal processing of amounts/numbers
To use the ml-number library you need to add it as a dependency to your project with the following:
npm install @mojaloop/ml-number
To create a new instance of the MlNumber class(returns instance of MlNumber):
const MlNumber = require('@mojaloop/ml-number')
const mlNumber = new MlNumber(6) // can be instantiated with a number
const mlNumber2 = new MlNumber('6') // can be instantiated with a string
const mlNumber3 = new MlNumber(mlNumber2) // can be instantiated with a MlNumber
Use different function(returns new instance of new MlNumber):
const MlNumber = require('@mojaloop/ml-number')
const originalValue = new MlNumber(6) // = "{"MlNumber": "6"}"
const additionResult = originalValue.add(12) // = "{"MlNumber": "18"}"
const multiplyResult = originalValue.multiply('12') // = "{"MlNumber": "72"}"
const divisionResult = originalValue.divide(2) // = "{"MlNumber": "3"}"
const subtractionResult = originalValue.subtract('2') // = "{"MlNumber": "4"}"
const mixedFunctionsValuesResult = new MlNumber('2').add(originalValue).sumList([additionResult, 5]) // = "{"MlNumber": "31"}"
const differentFunctionsResult = originalValue.add(2).multiply(3).divide(2).subtract(2) // = "{"MlNumber": "10"}"
const sumListResult = originalValue.sumList([2,3]) // = "{"MlNumber": "11"}"
const toStringResult = additionResult.toString() // = "18"
const toNumberResult = multiplyResult.toNumber() // = 72
const toFixedResult = divisionResult.toFixed() // = "3.0000"
const toFixedWithDecimalPlaceResult = subtractionResult.toFixed(7) // = "4.0000000"
To run the unit tests locally
npm test
To run the test coverage tests locally
npm run test:coverage