UNPKG

@datalayer/core

Version:
22 lines (21 loc) 789 B
/* * Copyright (c) 2023-2025 Datalayer, Inc. * Distributed under the terms of the Modified BSD License. */ const LOOKUP_VALUE_SYMBOL = [ { value: 1, symbol: "" }, { value: 1e3, symbol: "K" }, { value: 1e6, symbol: "M" }, { value: 1e9, symbol: "G" }, { value: 1e12, symbol: "T" }, { value: 1e15, symbol: "P" }, { value: 1e18, symbol: "E" }, ]; // https://stackoverflow.com/questions/9461621/format-a-number-as-2-5k-if-a-thousand-or-more-otherwise-900 export function numberFormatter(num, digits = 1) { const rx = /\.0+$|(\.[0-9]*[1-9])0+$/; const item = LOOKUP_VALUE_SYMBOL.slice().reverse().find(function (item) { return num >= item.value; }); return item ? (num / item.value).toFixed(digits).replace(rx, "$1") + item.symbol : "0"; }