将整数转换为“powernumber”。

huangapple go评论93阅读模式
英文:

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) =&gt; s.replace(/\d/g, d =&gt; &quot;⁰&#185;&#178;&#179;⁴⁵⁶⁷⁸⁹&quot;[d]);

// Example:
const s = &quot;test123&quot;
console.log(turnDigitsToSuperscript(s));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年8月5日 05:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839220.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定