动态百分比在数值范围内

huangapple go评论76阅读模式
英文:

Dynamic percentage in number range

问题

我有一系列产品的价格,从1美元到100美元不等。另外,我了解到对于成本为1美元的产品,将应用15%的利润率,而对于100美元的产品,将应用5%的利润率。

这意味着:

  • 1美元的产品将成本为1 + 1 * 0.15 = 1.15美元。
  • 100美元的产品将成本为100 + 100 * 0.05 = 105美元。

问题是如何计算介于1美元和100美元之间所有价格范围的利润率?

我尝试了不同的方法,但最终我不明白如何使这个计算具有动态性,以便我可以将公式应用于大范围的数据并获得结果。

英文:

I have a range of prices for multiple products from 1$ to 100$. As well, as I have understanging that for product with cost of 1$ is 15% of margin will apply and for 100$ product - 5%.

Mean that:
1. 1$ product will cost 1 + 1 * 0.15 = 1.15$
2. 100$ product will cost 100 + 100 * 0.05 = 105$

Question is how to calculate margin percentage for all range of numbers between 1$ and 100$?

I have tried to different approaches but in the end I just don't understand how to make this calculation dynamic, so I will apply formula to a big data range and get the result

答案1

得分: 1

你可以使用 delta 进行计算,并添加百分比的起始值。

基本上,你可以计算斜率,并将其与一个值相乘以获得结果。你还需要添加偏移量。在这里,你有一个递减(函数)线。

const
    fn = (price0, price1, percent0, percent1) => price =>
         (percent0 - percent1) * (price - price0) / (price1 - price0) + percent1;

console.log(...[1, 50.5, 100].map(fn(1, 100, 15, 5)));
英文:

You could calculate with delta and add the starting value of percent.

Basically you calculate the slope and multiply it with a value to get a result. You need to add the offset as well. Here you have a decreasing (function) line.

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
fn = (price0, price1, percent0, percent1) => price =>
(percent0 - percent1) * (price - price0) / (price1 - price0) + percent1;

console.log(...[1, 50.5, 100].map(fn(1, 100, 15, 5)));

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年3月31日 01:57:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定