Skip to content

Commit 21b3bc2

Browse files
committed
Improve calculator result for scientific notation
1 parent bd3326d commit 21b3bc2

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

kubejs/server_scripts/utils/calculator.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,17 @@ PlayerEvents.chat((event) => {
1414
let result = calculatorExec(ast);
1515

1616
if (typeof result === "number") {
17-
result = result.toFixed(5).replace(/\.?0+$/, "");
17+
result = result
18+
.toFixed(5)
19+
.replace(/e([\-+])?(\d+)$/, (_, sign, digits) => {
20+
sign = sign === "-" ? "⁻" : "";
21+
digits = digits
22+
.split("")
23+
.map((c) => "⁰¹²³⁴⁵⁶⁷⁸⁹"[+c])
24+
.join("");
25+
return ` × 10${sign}${digits}`;
26+
})
27+
.replace(/\.?0+$/, "");
1828
if (result === "-0") result = "0";
1929
}
2030

@@ -26,7 +36,7 @@ PlayerEvents.chat((event) => {
2636
Text.gray(" [Click to Copy]"),
2737
])
2838
.clickCopy(result)
29-
.hover(Text.of("Click to copy"))
39+
.hover(Text.of("Click to copy")),
3040
);
3141
} catch (e) {
3242
/** @type {string} */
@@ -37,7 +47,7 @@ PlayerEvents.chat((event) => {
3747
Text.aqua(message),
3848
Text.red(": "),
3949
Text.gray(error),
40-
])
50+
]),
4151
);
4252
}
4353
event.cancel();
@@ -88,7 +98,7 @@ let calculatorExec = (() => {
8898
let args = Array.from(arguments);
8999
if (args.length === 0) {
90100
throw new Error(
91-
"wrong argument count for max: expected 1 or more, got 0"
101+
"wrong argument count for max: expected 1 or more, got 0",
92102
);
93103
}
94104
if (args.some((a) => typeof a !== "number")) {
@@ -102,7 +112,7 @@ let calculatorExec = (() => {
102112
let args = Array.from(arguments);
103113
if (args.length === 0) {
104114
throw new Error(
105-
"wrong argument count for max: expected 1 or more, got 0"
115+
"wrong argument count for max: expected 1 or more, got 0",
106116
);
107117
}
108118
if (args.some((a) => typeof a !== "number")) {
@@ -132,7 +142,7 @@ let calculatorExec = (() => {
132142
": expected " +
133143
expectedArgs.length +
134144
", got " +
135-
args.length
145+
args.length,
136146
);
137147
}
138148
let argTypes = args.map((arg) => typeof arg);
@@ -145,7 +155,7 @@ let calculatorExec = (() => {
145155
expectedArgs.join(", ") +
146156
"], got [" +
147157
argTypes.join(", ") +
148-
"]"
158+
"]",
149159
);
150160
}
151161

@@ -166,14 +176,14 @@ let calculatorExec = (() => {
166176
validateArguments(
167177
"subtraction",
168178
["number", "number"],
169-
[left, right]
179+
[left, right],
170180
);
171181
return left - right;
172182
case "*":
173183
validateArguments(
174184
"multiplication",
175185
["number", "number"],
176-
[left, right]
186+
[left, right],
177187
);
178188
return left * right;
179189
case "/":
@@ -183,7 +193,7 @@ let calculatorExec = (() => {
183193
validateArguments(
184194
"floor division",
185195
["number", "number"],
186-
[left, right]
196+
[left, right],
187197
);
188198
return Math.floor(left / right);
189199
case "%":
@@ -193,7 +203,7 @@ let calculatorExec = (() => {
193203
validateArguments(
194204
"exponentiation",
195205
["number", "number"],
196-
[left, right]
206+
[left, right],
197207
);
198208
return Math.pow(left, right);
199209
}
@@ -459,7 +469,7 @@ function calculatorParse(tokens) {
459469
function expect(token) {
460470
if (!accept(token)) {
461471
throw new Error(
462-
"expected " + token + " got " + prettyToken(next()) + " instead"
472+
"expected " + token + " got " + prettyToken(next()) + " instead",
463473
);
464474
}
465475
}

0 commit comments

Comments
 (0)