Javascript 在javascript中计算百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37593889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:29:53  来源:igfitidea点击:

Calculate percentage in javascript

javascript

提问by John

I am trying to calculate in javascript. Some parts of the calculation are not working.

我正在尝试用 javascript 计算。计算的某些部分不起作用。

I want to calculate the total price with discount. For example: When price = 10.00 And discount = 50 (%)

我想用折扣计算总价。例如:当价格 = 10.00 且折扣 = 50 (%)

I need to calculate: 10.00 * 0,50

我需要计算:10.00 * 0,50

So I need something like this:

所以我需要这样的东西:

 var totalValue = numVal1 / 0, numVal2

When I do this, the script is not working. How can I fix this?

当我这样做时,脚本不起作用。我怎样才能解决这个问题?

This is my full code:

这是我的完整代码:

function getPrice() {         
 var numVal1 = Number(document.getElementById("price").value);
 var numVal2 = Number(document.getElementById("discount").value);

 var totalValue = numVal1 / numVal2
 document.getElementById("total").value = totalValue.toFixed(2);
}

回答by Jér?me

So you have a price ( 10 ) and a discount ( 50 )

所以你有一个价格( 10 )和一个折扣( 50 )

FinalPrice = price * ( 100-discount / 100 ) = 10 * ( 100-50 / 100 ) = 10 * 0.5 = 10 * 50%

FinalPrice = 价格 * ( 100-discount / 100 ) = 10 * ( 100-50 / 100 ) = 10 * 0.5 = 10 * 50%

So

所以

function getPrice() {         
 var numVal1 = Number(document.getElementById("price").value);
 var numVal2 = Number(document.getElementById("discount").value);

 var totalValue = numVal1 * ( (100-numVal2) / 100 )
 document.getElementById("total").value = totalValue.toFixed(2);
}

回答by Camilo Andres Mu?oz Bravo

I think that you are not calculating adecually the value, it should be:

我认为你不是在计算价值,它应该是:

var totalValue = numVal1 *(numVal2/100);

回答by LoremIpsum

You can't expect JavaScript to understand that 0, numVal2is supposed to be inferred to a floating number. The comma has its own meaning in JavaScript. Instead, you should treat numVal2as a percentage (which it is) and do :

你不能指望 JavaScript 理解0, numVal2应该被推断为一个浮点数。逗号在 JavaScript 中有自己的含义。相反,您应该将其numVal2视为百分比(它是)并执行以下操作:

var totalValue = numVal1 / (numVal2 / 100);

回答by Andrew Bone

To make it a percent you'd divided the discount by 100 and multiplied it by the price.

要使其成为百分比,您可以将折扣除以 100 再乘以价格。

For example I bought some shoes, £50 is the normal price but it was a closing down sale so 75% off. so we need to work out 75% of 50 and then take that off.

例如我买了一些鞋子,£50 是正常价格,但这是一个关闭销售所以 75% 的折扣。所以我们需要计算出 50 的 75%,然后把它去掉。

50 - (50 * 75 / 100) or 50 - (50 * 0.75)

50 - (50 * 75 / 100) 或 50 - (50 * 0.75)

(The shoes were only £12.50... bargain!)

(鞋子只要£12.50...便宜货!)

I would go for something like:

我会去做类似的事情:

getPrice = function() {
  var numVal1 = Number(document.getElementById("price").value);
  var numVal2 = Number(document.getElementById("discount").value) / 100;

  var totalValue = numVal1 - (numVal1 * numVal2)
  document.getElementById("total").value = totalValue.toFixed(2);
}
<input id="price">
<br>
<input id="discount">%
<br>
<button onclick="getPrice()">
  Get total
</button>
<br>
<input readonly id="total">