Skip to content

Commit 0231cee

Browse files
Dan pricing number format (#1630)
1 parent a13b4ad commit 0231cee

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

pgml-dashboard/src/components/inputs/range_group_pricing_calc/range_group_pricing_calc_controller.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { Controller } from "@hotwired/stimulus";
2+
import {
3+
numberToCompact,
4+
compactToNumber,
5+
} from "../../../../static/js/utilities/compact_number";
26

37
export default class extends Controller {
48
static targets = ["textInput", "range"];
@@ -18,7 +22,7 @@ export default class extends Controller {
1822
updateText(e) {
1923
if (e.detail >= this.minValue && e.detail <= this.maxValue) {
2024
this.removeErrorState();
21-
this.textInputTarget.value = e.detail;
25+
this.textInputTarget.value = numberToCompact(e.detail);
2226
this.updateDatasetValue();
2327
this.inputUpdated();
2428
} else {
@@ -27,20 +31,22 @@ export default class extends Controller {
2731
}
2832

2933
textUpdated() {
30-
let value = Number(this.textInputTarget.value);
34+
let value = compactToNumber(this.textInputTarget.value);
35+
3136
if (!value) {
32-
value = this.minValue;
33-
this.textInputTarget.value = value;
37+
this.textInputTarget.value = numberToCompact(this.minValue);
3438
}
3539

3640
if (value > this.maxValue || value < this.minValue) {
3741
this.applyErrorState();
3842
value = value > this.maxValue ? this.maxValue : this.minValue;
3943
value = value < this.minValue ? this.minValue : value;
44+
this.textInputTarget.value = numberToCompact(value);
4045
this.dispatchToRange(value);
4146
} else {
4247
this.removeErrorState();
4348
this.dispatchToRange(value);
49+
this.textInputTarget.value = numberToCompact(value);
4450
this.updateDatasetValue();
4551
this.inputUpdated();
4652
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
export const numberToCompact = (num) => {
3+
if (num >= 1e12) {
4+
return (num / 1e12).toFixed(1) + 'T'; // Trillion
5+
} else if (num >= 1e9) {
6+
return (num / 1e9).toFixed(1) + 'B'; // Billion
7+
} else if (num >= 1e6) {
8+
return (num / 1e6).toFixed(1) + 'M'; // Million
9+
} else if (num >= 1e3) {
10+
return (num / 1e3).toFixed(1) + 'K'; // Thousand
11+
} else {
12+
return num.toString(); // Less than a thousand
13+
}
14+
};
15+
16+
export const compactToNumber = (compact) => {
17+
const suffixes = { 'K': 1e3, 'M': 1e6, 'B': 1e9, 'T': 1e12 };
18+
const regex = /^(\d+(\.\d+)?)([KMBT])$/;
19+
20+
const match = compact.match(regex);
21+
if (match) {
22+
const number = parseFloat(match[1]);
23+
const suffix = match[3].toUpperCase();
24+
return number * suffixes[suffix];
25+
} else {
26+
return parseFloat(compact); // For numbers without suffixes
27+
}
28+
};

0 commit comments

Comments
 (0)