Working with currencies and financial data on JavaScript

JS
S
JavaScript

Simple snippet using a new SuperType 'BigNumber' via npm to handle financial data the proper way. BigNumber.js A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic. https://www.npmjs.com/package/bignumber.js

1import BigNumber from 'bignumber.js';
2
3const currentPrice = new BigNumber(value1);
4const newPrice = new BigNumber(value2);
5const dailyValue = currentPrice.dividedBy(30);
6const daysDiscount = dailyValue.times(newPrice);
7const negatedAmount = new BigNumber(transaction.amount).negated();
8
9const hrResult = daysDiscount.toFixed(2);

Created on 5/8/2018