将美元金额(小数)除以整数

发布于 2024-08-17 08:44:07 字数 269 浏览 4 评论 0原文

我需要为我正在构建的程序编写一个会计例程,该例程将使我能够将小数除以整数。例如:

$143.13 / 5 =

28.62
28.62
28.63
28.63
28.63

我在这里看到了这篇文章:Evenly split in c#,但似乎就像它只适用于整数除法一样。对于这个问题有什么优雅的解决方案吗?

I need to write an accounting routine for a program I am building that will give me an even division of a decimal by an integer. So that for example:

$143.13 / 5 =

28.62
28.62
28.63
28.63
28.63

I have seen the article here: Evenly divide in c#, but it seems like it only works for integer divisions. Any idea of an elegant solution to this problem?

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

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

发布评论

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

评论(7

似最初 2024-08-24 08:44:07

一次计算一个金额,然后从总数中减去每个金额,以确保您始终得到正确的剩余总数:

decimal total = 143.13m;
int divider = 5;
while (divider > 0) {
  decimal amount = Math.Round(total / divider, 2);
  Console.WriteLine(amount);
  total -= amount;
  divider--;
}

结果:

28,63
28,62
28,63
28,62
28,63

Calculate the amounts one at a time, and subtract each amount from the total to make sure that you always have the correct total left:

decimal total = 143.13m;
int divider = 5;
while (divider > 0) {
  decimal amount = Math.Round(total / divider, 2);
  Console.WriteLine(amount);
  total -= amount;
  divider--;
}

result:

28,63
28,62
28,63
28,62
28,63
述情 2024-08-24 08:44:07

您可以在不构建数组的情况下解决这个问题(以美分为单位):

int a = 100 * amount;
int low_value = a / n;
int high_value = low_value + 1;
int num_highs = a % n;
int num_lows = n - num_highs;

You can solve this (in cents) without constructing an array:

int a = 100 * amount;
int low_value = a / n;
int high_value = low_value + 1;
int num_highs = a % n;
int num_lows = n - num_highs;
赠意 2024-08-24 08:44:07

处理美分更容易。我建议您将 14313 分成 5 等份,而不是 143.13。这给你 2862 和余数 3。你可以将这个余数分配给前三个部分或任何你喜欢的方式。最后,将美分兑换回美元。

另请注意,您得到的余数总是小于您想要的零件数量。

It's easier to deal with cents. I would suggest that instead of 143.13, you divide 14313 into 5 equal parts. Which gives you 2862 and a remainder of 3. You can assign this remainder to the first three parts or any way you like. Finally, convert the cents back to dollars.

Also notice that you will always get a remainder less than the number of parts you want.

自由如风 2024-08-24 08:44:07

首先,确保您没有使用浮点数来表示美元和美分(请参阅其他帖子了解原因,但简单的原因是并非所有十进制数字都可以表示为浮点数,例如 $1.79)。

一种方法是:

decimal total = 143.13m;
int numberOfEntries = 5;
decimal unadjustedEntryAmount = total / numberOfEntries;
decimal leftoverAmount = total - (unadjustedEntryAmount * numberOfEntries);
int numberOfPenniesToDistribute = leftoverAmount * 100;
int numberOfUnadjustedEntries = numberOfEntries - numberOfPenniesToDistribute;

现在您有未调整的金额 28.62,然后您必须决定如何分配剩余部分。您可以从顶部或底部开始(看起来像您想要从底部开始)向每个人分配额外的一分钱。

for (int i = 0; i < numberOfUnadjustedEntries; i++) {
  Console.WriteLine(unadjustedEntryAmount);
}

for (int i = 0; i < numberOfPenniesToDistribute; i++) {
  Console.WriteLine(unadjustedEntryAmount + 0.01m);
}

您还可以将整个余数添加到第一个或最后一个条目。最后,根据会计需要,您还可以为其余部分创建单独的交易。

First of all, make sure you don't use a floating point number to represent dollars and cents (see other posts for why, but the simple reason is that not all decimal numbers can be represented as floats, e.g., $1.79).

Here's one way of doing it:

decimal total = 143.13m;
int numberOfEntries = 5;
decimal unadjustedEntryAmount = total / numberOfEntries;
decimal leftoverAmount = total - (unadjustedEntryAmount * numberOfEntries);
int numberOfPenniesToDistribute = leftoverAmount * 100;
int numberOfUnadjustedEntries = numberOfEntries - numberOfPenniesToDistribute;

So now you have the unadjusted amounts of 28.62, and then you have to decide how to distribute the remainder. You can either distribute an extra penny to each one starting at the top or at the bottom (looks like you want from the bottom).

for (int i = 0; i < numberOfUnadjustedEntries; i++) {
  Console.WriteLine(unadjustedEntryAmount);
}

for (int i = 0; i < numberOfPenniesToDistribute; i++) {
  Console.WriteLine(unadjustedEntryAmount + 0.01m);
}

You could also add the entire remainder to the first or last entries. Finally, depending on the accounting needs, you could also create a separate transaction for the remainder.

猫弦 2024-08-24 08:44:07

如果你有一个保证精确两位数精度的浮点数,那么这个(伪代码)怎么样:

amount = amount * 100 (convert to cents)
int[] amounts = new int[divisor]
for (i = 0; i < divisor; i++) amounts[i] = amount / divisor
extra = amount % divisor
for (i = 0; i < extra; i++) amounts[i]++

然后用 amounts 做任何你想做的事情,以分为单位 - 你可以转换回浮点数,如果你绝对必须,或者格式为美元和美分。

如果不清楚的话,所有这一切的目的不仅仅是平均分配浮动值,而是尽可能平均分配货币金额,因为美分是美元不可分割的单位。致OP:如果这不是您想要的,请告诉我。

If you have a float that is guaranteed exactly two digits of precision, what about this (pseudocode):

amount = amount * 100 (convert to cents)
int[] amounts = new int[divisor]
for (i = 0; i < divisor; i++) amounts[i] = amount / divisor
extra = amount % divisor
for (i = 0; i < extra; i++) amounts[i]++

and then do whatever you want with amounts, which are in cents - you could convert back to floats if you absolutely had to, or format as dollars and cents.

If not clear, the point of all this is not just to divide a float value evenly but to divide a monetary amount as evenly as possible, given that cents are an indivisible unit of USD. To the OP: let me know if this isn't what you wanted.

想你的星星会说话 2024-08-24 08:44:07

还可以使用 C# 迭代器生成来生成 Guffa的回答更方便:

public static IEnumerable<decimal> Divide(decimal amount, int numBuckets)
{
    while(numBuckets > 0)
    {
        // determine the next amount to return...
        var partialAmount = Math.Round(amount / numBuckets, 2);
        yield return partialAmount;
        // reduce th remaining amount and #buckets
        // to account for previously yielded values
        amount -= partialAmount;
        numBuckets--;
    }
}

It is also possible to use C# iterator generation to make Guffa's answer more convenient:

public static IEnumerable<decimal> Divide(decimal amount, int numBuckets)
{
    while(numBuckets > 0)
    {
        // determine the next amount to return...
        var partialAmount = Math.Round(amount / numBuckets, 2);
        yield return partialAmount;
        // reduce th remaining amount and #buckets
        // to account for previously yielded values
        amount -= partialAmount;
        numBuckets--;
    }
}
川水往事 2024-08-24 08:44:07

您可以使用您所引用的问题中的算法,方法是乘以 100,使用整数均匀除法函数,然后将每个结果除以 100(假设您只想处理 2 dp,如果您想要 3dp 乘以 1000 ETC)

You can use the algorithm in the question you're referencing by multipling by 100, using the integer evenly divide function, and then dividing each of the results by 100 (assuming you only want to handle 2 dp, if you want 3dp multiple by 1000 etc)

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