如何将数字向下舍入并删除JS中的数字?

时间:2017-12-01 22:34:51

标签: javascript html

我在JS上非常糟糕,所以我知道答案很简单,但无论我尝试什么都会破坏我的剧本。目前我有这段代码:

    var serverRequest = "https://api.roleplay.co.uk/v1/statistics";
    var serverData = "";

    function numberWithCommas(x) {
      return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }

    function success(data) {
        document.getElementById("playersTotal").innerHTML = "<span>Total Players: </span>" + numberWithCommas(data['totalplayers']);
        document.getElementById("housesTotal").innerHTML = "<span>Total Houses: </span>" + numberWithCommas(data['totalhouses']);
        document.getElementById("housesTotal").innerHTML = "<span>Total Houses: </span>" + numberWithCommas(data['totalhouses']);
        document.getElementById("moneyTotal").innerHTML = "<span>GDP: </span> £" + numberWithCommas(data.economy['totalcashonhand'] + data.economy['totalcashinbank']);
    }

    $.ajax({
      dataType: 'json',
      url: serverRequest,
      data: serverData,
      success: success
    });
<!DOCTYPE html>
<html>
    <head>
        <title>Server Stats - Stats Page</title>
        <link href="../css/style.css" type="text/css" rel="stylesheet">
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
        <link href="https://fonts.googleapis.com/css?family=Ubuntu:400,500" rel="stylesheet">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
        <script type="text/javascript" src="../javascript/server.js"></script>
    </head>
    <body class="leaderboards">
        <div class="header">
          <a href="leaderboards.html">Leaderboards</a>
          <a href="#" class="active">Server stats</a>
          <a href="../index.html">Search</a>
        </div>
        <div class="playerdata">
            <div class="head">
                <h1><span>Server</span> Statistics</h1>
            </div>
            <div class="server">
                <div>
                    <h2 id="playersTotal">...</h2>
                    <h2 id="housesTotal">...</h2>
                    <div>
                        <h2 id="moneyTotal">...</h2>
                    </div>
                </div>
                <div class="right">
                    <h2>...</h2>
                </div>
            </div>
        </div>
    </body>
</html>

“totalMoney”ID通常最终会超过1000亿英镑 - 我希望它将其缩小到最接近的十亿,然后从那里拿走1000亿,所以我的最终数字只会是前三位数轻松写“113亿”而不是“113,666,850,265,875,200,771”

编辑:我发现了一些适用于基本数学值的代码,但是使用我的API中的数字我得到一个NaN值而不是应该变成一个缩写值?代码如下:

    var serverRequest = "https://api.roleplay.co.uk/v1/statistics";
var serverData = "";

function numberWithCommas(x) {
  return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

function abbreviateNumber(value) {
var newValue = value;
if (value >= 1000) {
    var suffixes = ["", "k", "m", "b","t"];
    var suffixNum = Math.floor( (""+value).length/3 );
    var shortValue = '';
    for (var precision = 2; precision >= 1; precision--) {
        shortValue = parseFloat( (suffixNum != 0 ? (value / Math.pow(1000,suffixNum) ) : value).toPrecision(precision));
        var dotLessShortValue = (shortValue + '').replace(/[^a-zA-Z 0-9]+/g,'');
        if (dotLessShortValue.length <= 2) { break; }
    }
    if (shortValue % 1 != 0)  shortNum = shortValue.toFixed(1);
    newValue = shortValue+suffixes[suffixNum];
}
return newValue;

}

function success(data) {
    document.getElementById("playersTotal").innerHTML = "<span>Total Players: </span>" + numberWithCommas(data['totalplayers']);
    document.getElementById("housesTotal").innerHTML = "<span>Total Houses: </span>" + numberWithCommas(data['totalhouses']);
    document.getElementById("housesTotal").innerHTML = "<span>Total Houses: </span>" + numberWithCommas(data['totalhouses']);
    document.getElementById("moneyTotal").innerHTML = "<span>GDP: </span> £" + numberWithCommas(abbreviateNumber(data.economy['totalcashonhand'] + data.economy['totalcashinbank']));
}

$.ajax({
  dataType: 'json',
  url: serverRequest,
  data: serverData,
  success: success
});

2 个答案:

答案 0 :(得分:0)

我认为您最好的选择是在将值传递给numberWithCommas之前将其保持原样:

var moneyTotalInBillions = Math.floor((data.economy['totalcashonhand'] + data.economy['totalcashinbank']) / 1000000000);
document.getElementById("moneyTotal").innerHTML = "<span>GDP: </span> £" + numberWithCommas(moneyTotalInBillions);

首先将moneyTotal加在一起,将其除以10亿以获得数十亿,然后使用Math.floor删除小数点后的所有内容。将整数添加到innerHTML字符串会将整数转换为字符串并将它们一起添加。

你需要检查并确保moneyTotalInBillions是一个大于十亿的数字,否则你最终会得到£0。

答案 1 :(得分:0)

你说你想要四舍五入到最接近的十亿。

const number = 113666850265875200771;
const billion = Math.floor(number / 1e9);
console.log(billion + ' Billion'); //113666850265 Billion

但是,我认为你实际上想要四舍五入到最近的五分之一。

const number = 113666850265875200771;
const quintillion = Math.floor(number / 1e18);
console.log(quintillion + ' Quintillion'); //113 Quintillion

如需更多大号,请参阅此图表:Name of Large Numbers