如何划分一组数字,使其始终在0到100之间?

时间:2015-07-07 05:49:55

标签: javascript math

我有一组数字,其最大值可以是数千,我希望它在0到100之间等效。以便在Bootstrap progress bar中使用它。

$scope.events.stabilized = function() {
    console.warn('Event "stabilized" triggered');
    console.log.apply(console, arguments);
    if ($scope.visProgress >= 90) {
        $scope.visAvailable = true;
        $scope.visProgress = 100;
    }
};

$scope.events.stabilizationProgress = function() {
    $scope.visAvailable = false;
    /*var total = 100, iter = Math.round(arguments[0].iterations/100);
    if (typeof arguments[0] != "undefined") {
        total = arguments[0].total;
    }*/
    $scope.visProgress = Math.round(arguments[0].iterations / 10);
    //console.log(arguments[0].total + ", " + arguments[0].iterations+", "+$scope.visProgress);
    console.warn('Event "stabilizationProgress" triggered');
    console.log.apply(console, arguments);
};

$scope.events.stabilizationIterationsDone = function() {
    console.warn('Event "stabilizationIterationsDone" triggered');
    console.log.apply(console, arguments);
    $scope.visAvailable = true;
    $scope.visProgress = 100;
};

控制台输出

 Event "stabilized" triggered
 Object {iterations: 0}
 Event "stabilizationProgress" triggered
 Object {iterations: 50, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 100, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 150, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 200, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 250, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: , total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: , total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 400, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 450, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 500, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 550, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 600, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 650, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 700, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 750, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 800, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 850, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 900, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 950, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1000, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1050, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1100, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1150, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1200, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1250, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1400, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1450, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1500, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1550, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1600, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1650, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1700, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1750, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1800, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1850, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1900, total: 2000}
 Event "stabilizationProgress" triggered
 Object {iterations: 1950, total: 2000}
 Event "stabilizationIterationsDone" triggered
 Event "stabilized" triggered
 Object {iterations: 2001}

2 个答案:

答案 0 :(得分:2)

解决方案似乎很简单,在数学中:取值的百分比,你的计算几乎是正确的,只需添加几个paranthesis:

Math.round((arguments[0].iterations / arguments[0].total)*100);

所以如果你认为你有这个案子:

Event "stabilizationProgress" triggered
 Object {iterations: 900, total: 2000}

计算应该是:

Math.round((arguments[0].iterations / arguments[0].total)*100);
Math.round((900 / 2000)*100);
Math.round((9 / 20)*100);
Math.round((0.45)*100);
=45

原始计算结果如下:

Math.round(arguments[0].iterations / arguments[0].total)*100;
Math.round(900 / 2000)*100
Math.round(0.45)*100

<强>原因:
1)Math.round()方法将给出0表示Math.round(0.45){即一个值&lt; 0.5但大于0},这就是你得到的原因:0 * 100 = 0。

2)对于某些值,如Math.round(0.75){value&gt; 0.5但是&lt; 1}这个方法将返回1,这就是你得到的原因:1 * 100 = 100

在Essence中,你的公式只会给你两个0或100的值。有意义吗?

<强>参考: Math.round()

答案 1 :(得分:0)

在数学上,将每个数字除以最大值,然后乘以100。