【问题标题】:Modifying number based on % change根据百分比变化修改数字
【发布时间】:2018-01-18 11:12:00
【问题描述】:

我正在尝试根据可用供应量创建价格。例如,如果我们有 4000 万的商品库存,底价为 20 美元,我需要当供应量每上升 0.125% 时价格下降 0.06%,每变化 0.125% 时价格上升 0.1%供应量下降。

这是我到目前为止所能想到的......

 public void AddDailyCrudeOil() {
      CrudeOilSupplyRussia += DailyCrudeOilAmountRussia;
      /* 
       * Every 0.125% increase in supply, has a 0.06% decrease in the price. 
       * Conversely, any 0.125% decrease in supply, has a 0.1% increase in price. 
       * At our base supply, 40M, 0.125% will be 50K, and the condition will be true as our daily supply delivery is 100k
       * */
      if (DailyCrudeOilAmountRussia > CrudeOilSupplyRussia * 0.00125f) {
          CrudeOilPriceRussia *= -0.0006f;
      }
  }

但是,当然,这只会工作一次,而不是渐进式的。

我被困在这个问题上的时间比我想承认的要长,非常欢迎任何指导。

【问题讨论】:

  • 这是化合物吗?那么从 40,000,000 到 40,050,000 是第一个 0.125%,但接下来的 0.125% 将是 50,062.50?还是始终是基数 40,000,000 的 0.125%?
  • 它将是基础 40,000,000 的 0.125%!对不起,我应该提到这一点。这基本上就是生产者每天增加的额外供应量。
  • 降价也一样? 0.06% 是从初始价格计算出来的,然后作为固定金额应用吗?因此,如果价格从 100 开始,那么第一个 0.125% 的供应增加将下降到 99.94,然后在接下来的 0.125% 下降到 99.88,依此类推(第二个仍然是 100 的 0.06%,而不是 99.94 的 0.06%? )
  • 是的,先生,这是正确的。 40,000,000 100.00 100.00 40,050,000 99.94 40,100,000 99.88 40,150,000 99.82 40,200,000 99.76 4050,000 99.70 40,300,000 99.64 40,350,000 99.58 40,400,000 99.52 40,450,000 99.46 40,500,000 99.40 希望这能让它更清楚!感谢您的宝贵时间。
  • 添加了一个我认为符合这些要求的答案...

标签: c# economics


【解决方案1】:

这样的?编写为 NUnit 测试并使用“应该”进行断言,这通过了。请注意,我使用小数来避免任何奇怪的舍入问题。

[TestFixture]
public class tmp
{
    [Test]
    public void test()
    {
        var baseAmount = 40000000M;
        var basePrice = 100;
        var percentIncreaseTrigger = 0.00125M;
        var percentDecreaseInPrice = 0.0006M;

        GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger,
                    percentDecreaseInPrice, 40050000)
            .ShouldBe(99.94M);

        GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger, 
                    percentDecreaseInPrice, 40100000)
            .ShouldBe(99.88M);

        GetNewPrice(baseAmount, basePrice, percentIncreaseTrigger, 
                    percentDecreaseInPrice, 40400000)
            .ShouldBe(99.52M);

    }

    private decimal GetNewPrice(decimal baseAmount, decimal basePrice, 
           decimal trigger, decimal decreaseBy, decimal newAmount)
    {
        //work out what the trigger % is as an absolute amount
        var triggerAmount = baseAmount * trigger;

        //work out the price decrease as an absolute amount of the base price
        var priceDecreaseAmount = basePrice * decreaseBy;

        //work out how much over the baseAmount we are
        var amountOver = newAmount - baseAmount;

        //work out how many times we can fit the 0.125% into this - 
        //ignore fractional parts
        var numberOfTriggers = Math.Floor(amountOver / triggerAmount);

        //multiply the priceDecrease amount up by this
        var totalToTakeOffPrice = priceDecreaseAmount * numberOfTriggers;

        var result = basePrice - totalToTakeOffPrice;

        return result;

    }
}

【讨论】:

  • 给我一分钟运行这个!提前谢谢你,因为它看起来对我来说是正确的!
  • 再看一遍,很快就会再次回复!非常感谢您!
  • 刚刚意识到我并没有在数量下降时增加价格的另一面......但只是添加一个检查 newAmount 更高/更低然后适当地改变值应该很漂亮微不足道的
【解决方案2】:

这个应该可以解决问题

public double practicalCase(double actualStock, double baseStock, double percentChange, double basePrice,
        double priceDecrease, double priceIncrease)
    {
        double changeAmount = baseStock * percentChange;
        double priceChanges = Math.Floor(Math.Abs(baseStock - actualStock) / changeAmount);

        if (actualStock > baseStock)
            return (basePrice - (priceDecrease * priceChanges));
        else
            return (basePrice + (priceIncrease * priceChanges));
    }

尝试了以下案例

    [Fact]
    public void TestPracticalCase()
    {
        Assert.Equal(100d, _propertyManager.practicalCase(actualStock: 40000000d, baseStock: 40000000d, percentChange: 0.00125d,
            basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));

        Assert.Equal(99.94d, _propertyManager.practicalCase(actualStock: 40050000d, baseStock: 40000000d, percentChange: 0.00125d,
            basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));

        Assert.Equal(101d, _propertyManager.practicalCase(actualStock: 39500000d, baseStock: 40000000d, percentChange: 0.00125d,
            basePrice: 100d, priceDecrease: 0.06d, priceIncrease: 0.1d));
    }

【讨论】:

    【解决方案3】:

    我会做一个线性函数,对于任何供应,它都会返回正确的价格。这为您提供了一个连续的价格函数,对于每 0.125% 的供应增加没有“阶梯”。如果您需要这些步骤,请忽略此答案。

    根据您的要求,您可能需要 2 个公式。一个用于 4000 万以上的供应,一个用于以下,因为它们看起来有不同的斜率。但是,您真的想要 4000 万供应量的价格曲线出现“拐点”吗?

      static void Main()
      {
            var price = GetPrice(40050000.0);
            Console.WriteLine(price);
      }
    
      private static double GetPrice(double supply)
      {
                if (supply > 40000000.0)
                {
                     double slope = -0.012 / 50000.0; // Decrease price by 0.012 for each 50000 increase in supply
                     double origin = 20 - (slope * 40000000.0); // Calculate the price at 0 supply given the price at 40 million supply
                     return origin + supply * slope; // Price at the given supply
                }
                else
                {
                     double slope = -0.02 / 50000.0; // Decrease price by 0.02 for each 50000 increase in supply
                     double origin = 20 - (slope * 40000000.0); // Calculate the price at 0 supply given the price at 40 million supply
                     return origin + supply * slope; // Price at the given supply
                }
      }
    

    【讨论】:

    • 感谢您的回复!在我的 Excel 计算中,在 3500 万个供应量下,价格应该是 22.10 美元,但从 Unity 你的价格应该是 26 美元?那是每 0.125% 下降 0.3% 而不是 0.1%。我该如何纠正呢?
    • 我已将答案更改为 20 美元,即 4000 万美元。我已将其扩展为在 4000 万以上和以下具有不同的斜率。它以 3500 万美元的价格提供 22 美元。我认为这是正确的。 500 万滴是 100 滴 50000 (0.125%)。所以这应该会增加 100 倍 0.1% 或 10%。
    猜你喜欢
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    • 1970-01-01
    • 2019-08-11
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多