0

Okay hear me out, i know this might be a dumb question and there is probably a nice easy solution to this, but english is not my native language and i can't for the love of me find out what to even search on google or here for this problem.

The gist of it is, that i have a really simple discord bot running on Nodejs with Discord.js V14. I have a const that is defined as an integer. I want to convert this integer to a "powernumber" (these: ³ ¹ ⁴)

Is there any way this is even possible in a clean way?

I didn't really try anything yet, since i don't even know where to start. But what im trying to do is basically this

const number = "3"

//some node js magic that converts ³ to ³

if the ³ gets output as a string, that would be optimal, since i want to use that in a nickname like for example:

const user = interaction.options.getMember('user')

user.setNickname('nickname${number}'

I again appologise for not being able to explain exactly what i want. As i said earlier, english is not my native language :/

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
  • You'll want to look at your post and then hit [edit] to fix all that bad markdown. That said: make it a power _where_? In an HTML string? In unicode text? What is going to actually display it? – Mike 'Pomax' Kamermans Aug 04 '23 at 21:42
  • I mean there's only 10 of those, you can just hardcode them. – DallogFheir Aug 04 '23 at 21:47
  • Does this answer your question? [How can I convert numbers into scientific notation?](https://stackoverflow.com/questions/11124451/how-can-i-convert-numbers-into-scientific-notation) – Andy Ray Aug 04 '23 at 21:50
  • 1
    Are you just asking how to do `'4'.replace('4', '⁴')` ? – Andy Ray Aug 04 '23 at 21:53

1 Answers1

1

You can use replace with a callback function that will read out the superscript from a string:

const turnDigitsToSuperscript = (s) => s.replace(/\d/g, d => "⁰¹²³⁴⁵⁶⁷⁸⁹"[d]);

// Example:
const s = "test123"
console.log(turnDigitsToSuperscript(s));
trincot
  • 317,000
  • 35
  • 244
  • 286