【问题标题】:Javascript: Auto Converting numbers to prefixesJavascript:自动将数字转换为前缀
【发布时间】:2016-01-15 05:09:38
【问题描述】:

我正在创建一个空闲游戏(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 模拟器具有我正在寻找的功能。

编辑:谢谢,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();
&lt;span id="count"&gt;test&lt;/span&gt;

希望对你有帮助!!

【问题讨论】:

  • 这些可怜的人要点击多少次?
  • 有一些自动点击升级。我会说高达一万亿左右。
  • 1,000,000,000 becomes 1 billion. ... 1,000,000,001 会变成什么?
  • 我数学不好,可以是 1.000000001,也可以是科学计数法。但我更喜欢它是短期的。 (onlineconversion.com/large_numbers.htm) 也可以四舍五入。

标签: javascript numbers


【解决方案1】:

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

【讨论】:

  • 谢谢,虽然我不希望它采用科学计数法!如果没有其他选项可供我使用,我将使用它。
  • 谢谢!这正是我要找的!
猜你喜欢
  • 2012-07-05
  • 2020-04-04
  • 2021-10-30
  • 1970-01-01
  • 2017-10-02
  • 1970-01-01
  • 2020-03-16
  • 2013-11-08
  • 2013-04-02
相关资源
最近更新 更多