#formating numbers to metric form

1 messages · Page 1 of 1 (latest)

rain helm
#

does anyone know how to format scoreboards, like instead of 10900 it would be 10.9k

tiny flame
#

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))
forest anchorBOT
tiny flame
#

formatMetricNumber(getScore(player, 'money')) if your using getScore function

#

@rain helm

rain helm
#

hey

#

ill try that

rain helm
tiny flame
#

u cant

rain helm
#

aw

#

ok

tiny flame
#

use script

rain helm
#

Kk

tiny flame
rain helm
#

Oh,

#

Got it

#

@tiny flame uxpecting ','

tiny flame
#

did u remove the console.logs?

rain helm
#

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`, ``)```
tiny flame
#

do ${formatMetricNumber(getScore(player, 'c'))}

#

@rain helm

rain helm
#
NaNundefined```
#

thats what it says

tiny flame
#

it say Nanundefined if the score is 0

#

wait

rain helm
#

set it to 1000 still says that for some reason

tiny flame
#
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))```
forest anchorBOT
tiny flame
#

@rain helm try this

rain helm
#

try that?

#

kk

#

only says 0 now

tiny flame
#

show your code

#

@rain helm

rain helm
#

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`, ``)```
rain helm
#

pretty long a lot of stuff combined but here

#

thats whole thing

tiny flame
rain helm
#

do i have to replace the other one?

tiny flame
#

no just add this

#

wait do this in different js file

rain helm
fiery hornetBOT
#
Debug Result

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

#
Debug Result

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

#
Debug Result

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

tiny flame
#

bruh

rain helm
#

uh

tiny flame
fiery hornetBOT
#
Debug Result

There are errors in this [code](#1086847274883219608 message):

message.js:50:15 - error TS2304: Cannot find name 'openSub1'.

50               openSub1(source)
                 ~~~~~~~~
message.js:127:17 - error TS2304: Cannot find name 'MT'.

127                 MT(player);
                    ~~
message.js:129:24 - error TS2304: Cannot find name 'Home'.

129                 return Home(player);
                           ~~~~
message.js:237:7 - error TS2304: Cannot find name 'data'.

237   if (data.initialSpawn && getScore(player, 'admin') >= 1) {
          ~~~~
message.js:252:17 - error TS2339: Property 'tell' does not exist on type 'Player'.

252   } else player.tell(`§cYou are not allowed to use this command!`);
                    ~~~~

tiny flame
rain helm
#

its working

#

it was the js ${formatMetricNumber(getScore('c', player))}

tiny flame
#

yea your getScore is different from mine

rain helm
#

yea

#

thx for the help

tiny flame
#

wc

summer meteor
#

we can use this for basic one ig ```js
const sn = n => " kmbtPEZY"[
Math.ceil( ( String(n).length / 3 ) - 1 ) ];