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

时间:2016-01-15 05:09:39

标签: javascript numbers

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

示例:

10 = 10

10000000变为100万。

1,000,000,000变为10亿

1,000,000,000,000变为1万亿

1.4万亿将是1400000000000000 它与this非常相似。

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

修改:谢谢,快速绘制

对于那些感兴趣的人:



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();
&#13;
<span id="count">test</span>
&#13;
&#13;
&#13;

希望它有所帮助!!

1 个答案:

答案 0 :(得分:0)

How can I convert numbers into scientific notation?

&#13;
&#13;
var clicks = 100000000000000000000000;
alert(clicks.toExponential());
&#13;
&#13;
&#13;

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

&#13;
&#13;
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();
&#13;
<span id="count">test</span>
&#13;
&#13;
&#13;