UNPKG

trade-data-generator

Version:

A library to simulate market depth, trade data, and OHLC candlestick generation for trading applications.

25 lines (19 loc) 757 B
const BigNumber = require("bignumber.js"); function formatDecimal(value, fixedDecimalPlaces = 8) { if (value === null || value === undefined || isNaN(value) || value === "") { return ""; } const num = new BigNumber(value); // Format to the fixed number of decimal places const formattedValue = num.toFixed(fixedDecimalPlaces); // Add commas for thousands separators const parts = formattedValue.split("."); // parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); // Remove unnecessary trailing zeros and the decimal point if there are no decimal places const trimmedValue = parts .join(".") .replace(/(\.\d*?[1-9])0+$/, "$1") .replace(/\.0*$/, ""); return trimmedValue; } module.exports = { formatDecimal };