不知道如何用以下方法计算非复合税和混合税?

发布于 2024-11-01 00:23:08 字数 1637 浏览 1 评论 0原文

下面的类只有一个方法返回特定百分比的除数,因此如果我传递 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

我不确定如何实施上述内容。

The following class just has one method that returns the divisor of a certain percent, so if I pass it 5, it will return 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);
    }
 }

My main method is defined as follows:

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);
}

In the above main method, I am starting off with 100 as the total and if all the taxes are compounded, it works fine, I get 117.7 and if I take 117.7 and remove the taxes, I get back to 100. For non compounded, I only need to add 1 to the very end and then divide the total by that, but I am not sure how to do it. Currently, when I am looping through, I am just adding the divisors, for example 0.1 + 0.07 = 0.17. Outside the loop, I need to add 1 to that to get 1.17 and then multiply or divide the total to either add or remove the tax respectively. Then there is the issue of the adjustments being compounded and non-compounded which gets more complicated. In this case, I need to do something like the following:

For example, Assume I have 3 taxes, 10, 7, and 3. 10 and 7 are compounded and 3 is non-compounded, so the formula is:

100 * (1+((1+0.10) * (1+0.07)−1)+((1+0.03)−1)) which simplifies to 100 * ((1+0.10) * ( (1+0.07)+0.03) = 120.70

I am not sure how to implement the above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难遇 2024-11-08 00:23:08

试试这个:

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);
}

Try this:

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);
}
一个人练习一个人 2024-11-08 00:23:08

如果我正确理解您的要求,我认为您只需要在迭代调整时保留单独的复合分数和非复合分数,然后在最后将它们组合起来。

decimal total = 100.0M;

decimal compounded = 1.0M;
decimal nonCompounded = 0.0M;
foreach(Adjustment a in adjustments)
{
    if(a.IsCompounded)
    {
        decimal value = (1.0m + a.Calculate(a.Amount));

        if (a.Add)
            compounded *= value;
        else
            compounded /= value;
    }
    else
    {
        decimal value = a.Calculate(a.Amount);

        if (a.Add)
            nonCompounded += value;
        else
            nonCompounded -= value;
    }
}

if (nonCompounded >= 0)
{
  total *= (compounded + nonCompounded);
}
else
{
  total *= (compounded / (1.0m - nonCompounded));
}

您只会将 a.Amount 传递给 a.Calculate 吗?然后,您可以从对象中读取金额,例如

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

,或者您可以将其改为属性 get,例如,

public decimal AmountFraction
{
    get
    {
        return (Amount / 100.0M);
    }
}

然后将其读取为 decimal value = a.AmountFraction; 即作为属性而不是函数称呼。

编辑:修改组合行以根据注释删除非复合分数。

If I understand your requirements correctly, I think you just need to keep separate compounded and non-compounded fractions as you iterate through the Adjustments and just combine them at the end.

decimal total = 100.0M;

decimal compounded = 1.0M;
decimal nonCompounded = 0.0M;
foreach(Adjustment a in adjustments)
{
    if(a.IsCompounded)
    {
        decimal value = (1.0m + a.Calculate(a.Amount));

        if (a.Add)
            compounded *= value;
        else
            compounded /= value;
    }
    else
    {
        decimal value = a.Calculate(a.Amount);

        if (a.Add)
            nonCompounded += value;
        else
            nonCompounded -= value;
    }
}

if (nonCompounded >= 0)
{
  total *= (compounded + nonCompounded);
}
else
{
  total *= (compounded / (1.0m - nonCompounded));
}

Will you only ever pass a.Amount into a.Calculate? You could read the amount from the object then, e.g.

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

or you could make this a property get instead, e.g.

public decimal AmountFraction
{
    get
    {
        return (Amount / 100.0M);
    }
}

which you'd then read as decimal value = a.AmountFraction; i.e. as a property not a function call.

Edit: modified the combination line to remove non-compounded fractions as per comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文