1 | const leftPadding = (size: number) => {
|
---|
2 | return (input: string) => {
|
---|
3 | if (input.length < size) {
|
---|
4 | return " ".repeat(size - input.length) + input;
|
---|
5 | }
|
---|
6 | return input;
|
---|
7 | };
|
---|
8 | };
|
---|
9 |
|
---|
10 | export const integerFormatter = (size: number) => {
|
---|
11 | const padding = leftPadding(size);
|
---|
12 | return (integer: number) => padding("" + integer);
|
---|
13 | };
|
---|
14 |
|
---|
15 | const { floor, log10, pow } = Math;
|
---|
16 | const numberOfDigits = (n: number) => floor(log10(n) + 1);
|
---|
17 |
|
---|
18 | export const keepSignificantDigits = (digits: number) => (value: number) => {
|
---|
19 | const valueDigits = numberOfDigits(value);
|
---|
20 | if (valueDigits > digits) {
|
---|
21 | const extraDigits = valueDigits - digits;
|
---|
22 | const magnitude = pow(10, extraDigits);
|
---|
23 | return value - (value % magnitude);
|
---|
24 | }
|
---|
25 | return value;
|
---|
26 | };
|
---|
27 |
|
---|
28 | export const floatFormatter = (size: number, fractionDigits: number) => {
|
---|
29 | const numberFormatter = new Intl.NumberFormat("en-US", {
|
---|
30 | maximumFractionDigits: fractionDigits,
|
---|
31 | minimumFractionDigits: fractionDigits,
|
---|
32 | useGrouping: false,
|
---|
33 | });
|
---|
34 |
|
---|
35 | const padding = leftPadding(size);
|
---|
36 |
|
---|
37 | return (float: number) => padding(numberFormatter.format(float));
|
---|
38 | };
|
---|