MCPcopy
hub / github.com/dubinc/dub / nFormatter

Function nFormatter

packages/utils/src/functions/nformatter.ts:1–41  ·  view source on GitHub ↗
(
  number?: number | bigint,
  opts: { digits?: number; full?: boolean } = {
    digits: 1,
  },
)

Source from the content-addressed store, hash-verified

1export function nFormatter(
2 number?: number | bigint,
3 opts: { digits?: number; full?: boolean } = {
4 digits: 1,
5 },
6) {
7 const num = number !== undefined ? Number(number) : undefined;
8
9 if (!num) {
10 return "0";
11 }
12
13 if (opts.full) {
14 return Intl.NumberFormat("en-US").format(num);
15 }
16
17 const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
18
19 if (num < 1) {
20 return num.toFixed(opts.digits).replace(rx, "$1");
21 }
22
23 const lookup = [
24 { value: 1, symbol: "" },
25 { value: 1e3, symbol: "K" },
26 { value: 1e6, symbol: "M" },
27 { value: 1e9, symbol: "G" },
28 { value: 1e12, symbol: "T" },
29 { value: 1e15, symbol: "P" },
30 { value: 1e18, symbol: "E" },
31 ];
32 var item = lookup
33 .slice()
34 .reverse()
35 .find(function (item) {
36 return num >= item.value;
37 });
38 return item
39 ? (num / item.value).toFixed(opts.digits).replace(rx, "$1") + item.symbol
40 : "0";
41}

Callers 15

enrichPlanWithTierDataFunction · 0.90
getLinksStandardsFunction · 0.90
getPartnersStandardsFunction · 0.90
PartnerProgramSummaryFunction · 0.90
LinksLimitAlertFunction · 0.90
ClicksSummaryFunction · 0.90
ClicksExceededFunction · 0.90
ProgramPayoutThankYouFunction · 0.90
DubWrappedFunction · 0.90
StatCardFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…