jQuery 按值计算百分比或按百分比计算值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35759408/
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
Calculate percentage by value or value by percentage
提问by Dejavu
I have 3 input field to calculate price and discount percentage. First field is for regular price. What I am trying to do is : If user changes percentage value, discount price will be calculated (2 decimals) and if user changes the discount price, percentage will be calculated.
我有 3 个输入字段来计算价格和折扣百分比。第一个字段是正常价格。我想要做的是:如果用户更改百分比值,将计算折扣价(小数点后两位),如果用户更改折扣价,将计算百分比。
At the moment I only succeeded calculating the percentage:
目前我只成功计算了百分比:
function calculatePrice() {
var percentage = $('input[name=\'percentage\']').val(),
price = $('input[name=\'price\']').val(),
calcPrice = price - ( (price/100) * percentage ),
discountPrice = calcPrice.toFixed(2);
$('input[name=\'discount\']').val(discountPrice);
}
function calculatePerc() {
var discountPrice = $('input[name=\'discount]\']').val(),
price = $('input[name=\'price\']').val(),
calcPerc = (price/100) * (price-discountPrice),
discountPerc = calcPerc.toFixed();
$('input[name=\'percentage\']').val(discountPerc);
}
My html :
我的 HTML :
<input type="text" name="price" value="1000">
<input type="text" name="percentage" onkeyup="calculatePrice()">
<input type="text" name="discount" onkeyup="calculatePerc()">
Here is my fiddle : https://jsfiddle.net/90bo9okg/
这是我的小提琴:https: //jsfiddle.net/90bo9okg/
回答by Roko C. Buljan
// Reusable helper functions
const calculateSale = (listPrice, discount) => {
listPrice = parseFloat(listPrice);
discount = parseFloat(discount);
return (listPrice - ( listPrice * discount / 100 )).toFixed(2); // Sale price
}
const calculateDiscount = (listPrice, salePrice) => {
listPrice = parseFloat(listPrice);
salePrice = parseFloat(salePrice);
return 100 - (salePrice * 100 / listPrice); // Discount percentage
}
// Our use case
const $list = $('input[name="list"]'),
$disc = $('input[name="disc"]'),
$sale = $('input[name="sale"]');
$list.add( $disc ).on('input', () => { // List and Discount inputs events
let sale = $list.val(); // Default to List price
if ( $disc.val().length ) { // if value is entered- calculate sale price
sale = calculateSale($list.val(), $disc.val());
}
$sale.val( sale );
});
$sale.on('input', () => { // Sale input events
let disc = 0; // Default to 0%
if ( $sale.val().length ) { // if value is entered- calculate the discount
disc = calculateDiscount($list.val(), $sale.val());
}
$disc.val( disc );
});
// Init!
$list.trigger('input');
List price: <input type="number" name="list" value="1000"><br>
Discount: <input type="number" name="disc">%<br>
Sale price: <input type="number" name="sale">
<script src="//code.jquery.com/jquery-3.3.1.min.js"></script>
回答by Dhaval Shah
function calculatePrice() {
var percentage = $('input[name=\'percentage\']').val(),
price = $('input[name=\'price\']').val(),
calcPrice = price - ((price / 100) * percentage),
discountPrice = calcPrice.toFixed(2);
$('input[name=\'discount\']').val(discountPrice);
}
function calculatePerc() {
var discountPrice = $('input[name=\'discount\']').val(),
price = $('input[name=\'price\']').val(),
calcPerc = (price - discountPrice) * 100 / price,
discountPerc = calcPerc.toFixed(2);
$('input[name=\'percentage\']').val(discountPerc);
}
回答by Eric Milliot-Martinez
For those looking for a small, quick, and easy piece of javascript code to calculate percentage and round it:
对于那些正在寻找一小段、快速且简单的 javascript 代码来计算百分比并对其进行四舍五入的人:
var percentage = Math.round(((currentValue / goalValue) * 100)) +"%";
var percentage = Math.round(((currentValue / goalValue) * 100)) +"%";
It will return "100" when the fraction returns "1".
当分数返回“1”时,它将返回“100”。