-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotation.js
More file actions
35 lines (27 loc) · 1.05 KB
/
Copy pathnotation.js
File metadata and controls
35 lines (27 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
"use strict";
const PREFIXES = ["K", "M", "B", "T", "q", "Q", "s", "S", "O", "N", "D"];
function commas(num) {
return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
function prefix(num, digits) {
const magnitude = Math.log10(num);
if (magnitude < digits + 2) return commas(num);
const illion = Math.floor(magnitude / 3);
const postfix = PREFIXES[illion - 1];
if (!postfix) return scientific(num, digits);
const number = Math.floor(num / 10**(illion * 3 - digits)) / 10**digits
return number.toString() + postfix;
}
function scientific(num, digits) {
const magnitude = Math.log10(num);
if (magnitude < digits + 2) return commas(num);
const postfix = `e${magnitude}`;
const number = Math.floor(num / 10**(magnitude - digits)) / 10**digits
return number.toString() + postfix;
}
export function notation(num, currency, digits = 6) {
let costText = prefix(num, digits);
if (currency === "resetPoint" && num > 0)
costText = "-" + costText;
return costText;
}