【问题标题】:How do you convert scientific notation to expanded string in Javascript? [duplicate]如何在 Javascript 中将科学记数法转换为扩展字符串? [复制]
【发布时间】:2020-12-27 15:10:44
【问题描述】:

例如:

// Current behaviour
1e+25.toString() // Becomes "1e+25"

// Want this instead
1e+25.toDecimalString() // Becomes "1000000000..."

【问题讨论】:

  • 1e+25 大于Number.MAX_SAFE_INTEGER,不能完全表示为Number。对于如此大的值,您应该使用 BigInt
  • @axiac 我不想表示为数字。仅将其转换为字符串 - 字符串没有限制。
  • 从什么转换?从一个数字,不是吗? 1e+25Number 类型的值。这与1e+25 + 1 和下一个十亿整数值相等(由于 JavaScript 存储数字的方式)。 Check it out on TIO。如果你不使用BigInt,你的方法从一开始就是无效的,你可能会在某个时候得到不正确和意想不到的结果。

标签: javascript bignum


【解决方案1】:

你可以使用 toLocaleString() 方法。试试这个:

1e+25.toLocaleString("en-US", { useGrouping: false })
// Output: "10000000000000000000000000"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 2021-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多