Javascript: 自动将数字转换为前缀

Javascript: Auto Converting numbers to prefixes

我正在创建一个闲置游戏(Cookie Clicker 等),似乎当我的玩家达到大量点击时,游戏开始变慢。 高数字也不适合游戏,因为它占用太多 space。那么是否有一个脚本可以将每个数字转换为 prefix?

示例:

10 = 10

10000000 变成 100 万。

1,000,000,000 变成 10 亿

1,000,000,000,000 变成 1 万亿

1.4 千万亿等于 1400000000000000
它与 this 非常相似。

Cookie Clicker 和 swarm 模拟器具有我正在寻找的功能。

编辑: 谢谢,Drew Quick

对于那些感兴趣的人:

var count = 1;

function main() {
  count += 1000;
  
var str = count.toString();
var tmpCount = '';
if (count < 1000000) {
    tmpCount = "";
} else if (count > 1000000 && count < 1000000000) {
    str = "Million";
    tmpCount = (count / 1000000).toFixed(2);
} else if (count > 1000000000 && count < 1000000000000) {
    str = "Billion";
    tmpCount = (count / 1000000000).toFixed(2);
} else if (count > 1000000000000 && count < 1000000000000000) {
    str = "Trillion";
    tmpCount = (count / 1000000000000).toFixed(2);
} else if (count > 1000000000000000 && count < 1000000000000000000) {
str = "Quadrillion";
    tmpCount = (count / 1000000000000000).toFixed(2);
}     else if (count > 1000000000000000000 && count < 1000000000000000000000) {
    str = "Quintillion";
    tmpCount = (count / 1000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000 && count < 1000000000000000000000000) {
    str = "Sextillion";
    tmpCount = (count / 1000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000 && count < 1000000000000000000000000000) {
    str = "Septillion";
    tmpCount = (count / 1000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000 && count < 1000000000000000000000000000000) {
    str = "Octillion";
    tmpCount = (count / 1000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000 && count < 1000000000000000000000000000000000) {
    str = "Nonillion";
    tmpCount = (count / 1000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000 && count < 1000000000000000000000000000000000000) {
    str = "Decillion";
    tmpCount = (count / 1000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000) {
    str = "Undecillion";
    tmpCount = (count / 1000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000) {
    str = "Duodecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000) {
    str = "Tredecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000) {
    str = "Quattuordecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000) {
    str = "Quindecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000) {
    str = "Sexdecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000) {
    str = "Septendecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000000) {
    str = "Octodecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000000 && count < 1000000000000000000000000000000000000000000000000000000000000000) {
    str = "Novemdecillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 1000000000000000000000000000000000000000000000000000000000000000 && count < 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) {
    str = "Vigintillion";
    tmpCount = (count / 1000000000000000000000000000000000000000000000000000000000000000).toFixed(2);
} else if (count > 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 && count) {
    str = "Googol";
    tmpCount = (count / 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000).toFixed(2);
}


  document.getElementById("count").innerText = tmpCount + ' ' + str;
  setTimeout(function() {
    main();
  }, 1);
}

main();
<span id="count">test</span>

希望对您有所帮助!!

How can I convert numbers into scientific notation?

var clicks = 100000000000000000000000;
alert(clicks.toExponential());

编辑:像这样简单直接的事情怎么样?顺便说一下,这很粗糙。只是为了传达这个想法。

var count = 1;

function main() {
  count += 1000;

  var str = count.toString();
  var tmpCount;
  if (count < 1000000) {
    tmpCount = "";
  } else if (count > 1000000 && count < 1000000000) {
    str = "million";
    tmpCount = (count / 1000000).toFixed(2);
  } else if (count > 1000000000 && count < 1000000000000) {
    str = "billion";
    tmpCount = (count / 1000000000).toFixed(2);
  }

  document.getElementById("count").innerText = tmpCount + ' ' + str;
  setTimeout(function() {
    main();
  }, 1);
}

main();
<span id="count">test</span>