从百分比计算损失和利润
Calculate loss and profit from percentage
FIDDLE
https://jsfiddle.net/o1hq1apw/2/
当前比特币价格为 2700 美元。
价格在 7 天内上涨了 +34%。
我持有3.011 BTC,如何计算我的收益?
currentPrice = 2700;
percent = 34;
holdings = 3.011;
alert( calcPec(currentPrice,percent,holdings) );
当前比特币价格为 2700 美元。
价格在 2 天内上涨了 -7%。
我持有3.011 BTC,如何计算损失?
currentPrice = 2700;
percent = -7;
holdings = 3.011;
alert( calcPec(currentPrice,percent,holdings) );
// This is what i have but it is not correct
function calcPec(currentPrice,percent,holdings)
{
res = currentPrice*percent/2;
sum = holdings*res;
return '$'+sum;
}
您忘记将百分比除以 100 得到一个分数。
// The current BTC price is 2700$.
// The price has increased by +34% in 7Days.
// I hold 3.011 BTC, how can i calculate my profit?
currentPrice = 2700;
percent = 34;
holdings = 3.011;
console.log(calcPec(currentPrice, percent, holdings));
// The current BTC price is 2700$.
// The price has increased by -7% in 2Days.
// I hold 3.011 BTC, how can i calculate my loss?
currentPrice = 2700;
percent = -7;
holdings = 3.011;
console.log(calcPec(currentPrice, percent, holdings));
function calcPec(currentPrice, percent, holdings) {
const curr = holdings * currentPrice;
const changed = curr * (1 + (percent / 100));
return '$' + (changed - curr);
}
将来您可能希望将百分比定义为分数,以避免出现此类错误。所以而不是 percent = 34
你会做 percent = 0.34
编辑 也修复了其他错误;
您可以将百分比值除以 100 得到变化的分数。
function calcPec(price, percent, holdings) {
return '$' + (holdings * price * percent / 100).toFixed(2);
}
console.log(calcPec(2700, 34, 3.011));
console.log(calcPec(2700, -7, 3.011));
所以你持有 3.011 BTC,目前每个 BTC 2700 美元。
7 天前 BTC 的价格等于 100%,现在它已经上涨了 34%,所以 2700$ 等于 134%。
要计算 7 天前的价格,您必须将 2700 除以 134%,即大约。 2014 美元。
所以你的收入是 (2700 - 2014) * 3.011 = 2065
您的代码应如下所示:
function calcPec(currentPrice, percent, holdings)
{
oldPrice = currentPrice / ((100 + percent) / 100);
sum = (currentPrice - oldPrice) * holdings;
}
FIDDLE https://jsfiddle.net/o1hq1apw/2/
当前比特币价格为 2700 美元。
价格在 7 天内上涨了 +34%。
我持有3.011 BTC,如何计算我的收益?
currentPrice = 2700;
percent = 34;
holdings = 3.011;
alert( calcPec(currentPrice,percent,holdings) );
当前比特币价格为 2700 美元。
价格在 2 天内上涨了 -7%。
我持有3.011 BTC,如何计算损失?
currentPrice = 2700;
percent = -7;
holdings = 3.011;
alert( calcPec(currentPrice,percent,holdings) );
// This is what i have but it is not correct
function calcPec(currentPrice,percent,holdings)
{
res = currentPrice*percent/2;
sum = holdings*res;
return '$'+sum;
}
您忘记将百分比除以 100 得到一个分数。
// The current BTC price is 2700$.
// The price has increased by +34% in 7Days.
// I hold 3.011 BTC, how can i calculate my profit?
currentPrice = 2700;
percent = 34;
holdings = 3.011;
console.log(calcPec(currentPrice, percent, holdings));
// The current BTC price is 2700$.
// The price has increased by -7% in 2Days.
// I hold 3.011 BTC, how can i calculate my loss?
currentPrice = 2700;
percent = -7;
holdings = 3.011;
console.log(calcPec(currentPrice, percent, holdings));
function calcPec(currentPrice, percent, holdings) {
const curr = holdings * currentPrice;
const changed = curr * (1 + (percent / 100));
return '$' + (changed - curr);
}
将来您可能希望将百分比定义为分数,以避免出现此类错误。所以而不是 percent = 34
你会做 percent = 0.34
编辑 也修复了其他错误;
您可以将百分比值除以 100 得到变化的分数。
function calcPec(price, percent, holdings) {
return '$' + (holdings * price * percent / 100).toFixed(2);
}
console.log(calcPec(2700, 34, 3.011));
console.log(calcPec(2700, -7, 3.011));
所以你持有 3.011 BTC,目前每个 BTC 2700 美元。 7 天前 BTC 的价格等于 100%,现在它已经上涨了 34%,所以 2700$ 等于 134%。 要计算 7 天前的价格,您必须将 2700 除以 134%,即大约。 2014 美元。 所以你的收入是 (2700 - 2014) * 3.011 = 2065
您的代码应如下所示:
function calcPec(currentPrice, percent, holdings)
{
oldPrice = currentPrice / ((100 + percent) / 100);
sum = (currentPrice - oldPrice) * holdings;
}