#formating numbers to metric form
1 messages · Page 1 of 1 (latest)
use this
function formatMetricNumber(number) {
const suffixes = ['', 'k', 'm', 'b', 't', 'P', 'E', 'Z', 'Y'];
const base = 1000;
const exponent = Math.min(Math.floor(Math.log10(Math.abs(number)) / 3), suffixes.length - 1);
const formattedNumber = (number / Math.pow(base, exponent)).toFixed(1);
return formattedNumber + suffixes[exponent];
}
console.log(formatMetricNumber(2));
console.log(formatMetricNumber(1000))
console.log(formatMetricNumber(1000000))
console.log(formatMetricNumber(1000000000))
2.0
1.0k
1.0m
1.0b
formatMetricNumber(getScore(player, 'money')) if your using getScore function
@rain helm
how exactly would i put this on a titleraw scoreboard?
u cant
use script
Kk
i mean u cant in commands but in script it can
did u remove the console.logs?
ok nvm its working
@tiny flame just displays the text do you know whats wrong?
.textField(`§eGive Coins to A Player!\n \n§eYour Wallet:§6 formatMetricNumber(getScore(player, 'c'))`, `Only Use Numbers`, ``)```
set it to 1000 still says that for some reason
function formatMetricNumber(value) {
const types = ["", "k", "M", "B", "T", "P", "E", "Z", "Y"];
const selectType = (Math.log10(value) / 3) | 0;
if (selectType == 0) return value;
let scaled = value / Math.pow(10, selectType * 3);
return scaled.toFixed(1) + types[selectType];
}
console.log(formatMetricNumber(0))
console.log(formatMetricNumber(1000))```
0
1.0k
@rain helm try this
i did what you just put
just says 0 for me
and this is the display thingy
.textField(`§eGive Coins to A Player!\n \n§eYour Wallet:§6 ${formatMetricNumber(getScore(player, 'c'))}`, `Only Use Numbers`, ``)```
show full code
add this getScore function js function getScore(player, objectiveId, rNull = false) { try { return world.scoreboard.getObjective(objectiveId).getScore(player.scoreboard); } catch (error) { return rNull ? null : 0; } }
do i have to replace the other one?
JavaScript/TypeScript code blocks not detected in [message](#1086847274883219608 message ).
You can either send the script in code block highlighted in JS format:
```js
console.log("Hi mom!")
``\nor attach a file end in .js
JavaScript/TypeScript code blocks not detected in [message](#1086847274883219608 message ).
You can either send the script in code block highlighted in JS format:
```js
console.log("Hi mom!")
``\nor attach a file end in .js
JavaScript/TypeScript code blocks not detected in [message](#1086847274883219608 message ).
You can either send the script in code block highlighted in JS format:
```js
console.log("Hi mom!")
``\nor attach a file end in .js
bruh
uh
There are errors in this [code](#1086847274883219608 message):
[36mmessage.js[0m:[33m50[0m:[33m15[0m - [31merror[0m[30m TS2304: [0mCannot find name 'openSub1'.
[7m50[0m openSub1(source)
[7m [0m [31m ~~~~~~~~[0m
[36mmessage.js[0m:[33m127[0m:[33m17[0m - [31merror[0m[30m TS2304: [0mCannot find name 'MT'.
[7m127[0m MT(player);
[7m [0m [31m ~~[0m
[36mmessage.js[0m:[33m129[0m:[33m24[0m - [31merror[0m[30m TS2304: [0mCannot find name 'Home'.
[7m129[0m return Home(player);
[7m [0m [31m ~~~~[0m
[36mmessage.js[0m:[33m237[0m:[33m7[0m - [31merror[0m[30m TS2304: [0mCannot find name 'data'.
[7m237[0m if (data.initialSpawn && getScore(player, 'admin') >= 1) {
[7m [0m [31m ~~~~[0m
[36mmessage.js[0m:[33m252[0m:[33m17[0m - [31merror[0m[30m TS2339: [0mProperty 'tell' does not exist on type 'Player'.
[7m252[0m } else player.tell(`§cYou are not allowed to use this command!`);
[7m [0m [31m ~~~~[0m
try ${formatMetricNumber(getScore('c', player))}
remove this
yea your getScore is different from mine
wc
we can use this for basic one ig ```js
const sn = n => " kmbtPEZY"[
Math.ceil( ( String(n).length / 3 ) - 1 ) ];