获取一个数字的百分比 javascript

Get the percentage of a number with javascript

所以我正在创建一个网络应用程序并获得了一些数据。

您现在有积分(现在=284)和总积分(总=1000)。所以,它们之间的区别是 dif=716.

如何使用 javascript 将差值转换为百分比值,例如 32% 或其他值?

谢谢

var total = 1000,
subtract = 284;
var differencePercentage = ((total - subtract) / total) * 100; // 71.6

这是一道数学题,不是编程题,但是你问javascript这里你是一个例子:

var value = 249;
var total = 1000;

var calcPercent = function(v, t) {
  return 100*v/t;
};

alert(calcPercent(value, total)+"%");

简单的数学运算就是

var now = 284;
var total = 1000;
console.log( "percentage is " + (now* 100/total) );
console.log( "Negative percentage is " + (100-(now* 100/total)) );

(function(){
  var now = 284;
  var total = 1000;

  var difference = ((total - now) / total) * 100;
  
  alert(difference);
})()