Amb*_*ite 16 c# accounting division
我需要为我正在构建的程序编写一个会计例程,它将给出一个十进制的偶数除以整数.例如:
$143.13 / 5 =
28.62
28.62
28.63
28.63
28.63
Run Code Online (Sandbox Code Playgroud)
我在这里看过这篇文章:在c#中均匀划分,但似乎它只适用于整数除法.想知道这个问题的优雅解决方案吗?
Guf*_*ffa 33
一次计算一个金额,然后从总金额中减去每个金额,以确保始终保留正确的总金额:
decimal total = 143.13m;
int divider = 5;
while (divider > 0) {
decimal amount = Math.Round(total / divider, 2);
Console.WriteLine(amount);
total -= amount;
divider--;
}
Run Code Online (Sandbox Code Playgroud)
结果:
28,63
28,62
28,63
28,62
28,63
Run Code Online (Sandbox Code Playgroud)
Mar*_*tos 30
您可以在不构建数组的情况下解决此问题(以分为单位):
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;
Run Code Online (Sandbox Code Playgroud)