英文:
How to convert an Integer to a "powernumber"
问题
我知道这可能是一个愚蠢的问题,而且可能有一个简单的解决方案,但是英语不是我的母语,我找不到在Google或这里搜索这个问题的方法。
问题的要点是,我有一个在Node.js上运行的非常简单的Discord机器人,使用Discord.js V14定义为整数的常量。我想将这个整数转换为一个“powernumber”(这些:³ ¹ ⁴)
有没有办法以一种干净的方式做到这一点?
我还没有真正尝试过任何事情,因为我甚至不知道从哪里开始。但我想做的基本上是这样的:
const number = "3"
// 一些Node.js魔法,将³转换为³
如果³以字符串形式输出,那将是最理想的,因为我想在昵称中使用它,例如:
const user = interaction.options.getMember('user')
user.setNickname(`nickname${number}`)
我再次为无法准确解释我的意思而道歉。正如我之前所说,英语不是我的母语:/
英文:
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 :/
答案1
得分: 1
你可以使用 replace
与一个回调函数,该函数将从字符串中读取出上标:
const turnDigitsToSuperscript = (s) => s.replace(/\d/g, d => "⁰¹²³⁴⁵⁶⁷⁸⁹"[d]);
// 例子:
const s = "test123"
console.log(turnDigitsToSuperscript(s));
英文:
You can use replace
with a callback function that will read out the superscript from a string:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const turnDigitsToSuperscript = (s) => s.replace(/\d/g, d => "⁰¹²³⁴⁵⁶⁷⁸⁹"[d]);
// Example:
const s = "test123"
console.log(turnDigitsToSuperscript(s));
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论