gpt4 book ai didi

c# - 无法弄清楚如何使用以下方法计算非复合税和混合税?

转载 作者:太空宇宙 更新时间:2023-11-03 16:46:46 24 4
gpt4 key购买 nike

下面的类只有一个方法返回特定百分比的除数,所以如果我将 5 传递给它,它将返回 0.05。

public class Adjustment
{
public decimal Amount {get;set;}
public bool IsCompounded {get;set;}
public bool Add{get;set;}

public decimal Calculate(decimal amount)
{
return (amount / 100.0M);
}
}

我的主要方法定义如下:

void Main()
{

Adjustment a1 = new Adjustment {Amount = 10.0M, IsCompounded = false, Add = true};
Adjustment a2 = new Adjustment {Amount = 7.0M, IsCompounded = false, Add = true};


List<Adjustment> adjustments = new List<Adjustment>();
adjustments.Add(a1);
adjustments.Add(a2);

decimal value = 0.0M;
decimal total = 100.0M;

foreach(Adjustment a in adjustments)
{
if(a.IsCompounded)
{
value = (1 + a.Calculate(a.Amount));

if(a.Add)
total *= value;
else
total /= value;
}
else if(!a.IsCompounded)
{
value += a.Calculate(a.Amount);

if(a.Add)
total *= value;
else
total /= value;
}
}

Console.WriteLine(total);
}

在上面的主要方法中,我从 100 作为总数开始,如果所有的税都是复合的,它工作正常,我得到 117.7,如果我取 117.7 并去除税,我回到 100。对于非复合,我只需要在最后加 1,然后将总数除以它,但我不知道该怎么做。目前,当我循环时,我只是添加除数,例如 0.1 + 0.07 = 0.17。在循环之外,我需要将其加 1 以获得 1.17,然后乘以或除以总数以分别添加或删除税款。然后是复合和非复合调整的问题变得更加复杂。在这种情况下,我需要执行以下操作:

例如,假设我有 3 种税,10、7 和 3。10 和 7 是复利的,3 是非复利的,所以公式是:

100 * (1+((1+0.10) * (1+0.07)−1)+((1+0.03)−1)) 简化为 100 * ((1+0.10) * ( (1+ 0.07)+0.03) = 120.70

我不确定如何实现上面的内容。

最佳答案

试试这个:

public class Adjustment
{
public decimal Amount {get;set;}
public bool IsCompounded {get;set;}
public bool Add{get;set;}

public decimal Calculate()
{
if(IsCompounded)
return 1 + Amount / 100.0M;
else
return Amount / 100.0M;
}
}

void Main()
{
List<Adjustment> adjustments = new List<Adjustment>();
adjustments.Add(new Adjustment() {Amount = 10.0M, IsCompounded = false, Add = true});
adjustments.Add(new Adjustment() {Amount = 7.0M, IsCompounded = false, Add = true};);

decimal value = 0.0M;
decimal total = 100.0M;

foreach(Adjustment a in adjustments)
{
value = a.Calculate();

if(a.IsCompound)
{
if(a.Add)
total *= value;
else
total /= value;
}
else
{
if(a.Add)
total += value; //should be a sum for nom-compounded
else
total -= value;
}
}

Console.WriteLine(total);
}

关于c# - 无法弄清楚如何使用以下方法计算非复合税和混合税?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666604/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com