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

Posted

技术标签:

【中文标题】Javascript:自动将数字转换为前缀【英文标题】:Javascript: Auto Converting numbers to prefixes 【发布时间】: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) 也可以四舍五入。 【参考方案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;

【讨论】:

谢谢,虽然我不希望它采用科学计数法!如果没有其他选项可供我使用,我将使用它。 谢谢!这正是我要找的!

以上是关于Javascript:自动将数字转换为前缀的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript--数据类型转换

javascript中字符串向数字类型的自动转换

Javascript:将字符串转换为数字?

将Javascript数字转换为两位小数[重复]

sh 按文件名顺序将图像转换为PDF,文本前缀和数字后缀,如Untitled-0.jpg,Untitled-1.jpg等。

用JavaScript将数字转换为大写金额