用一个整数整除美元金额(十进制) [英] Evenly divide a dollar amount (decimal) by an integer
问题描述
我需要编写一个程序一个会计程序我建立,这将使我十进制的,甚至除以一个整数。因此例如:
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
我看到这里的文章:<一HREF =http://stackoverflow.com/questions/577427/evenly-divide-in-c> http://stackoverflow.com/questions/577427/evenly-divide-in-c 时,但它好像它仅适用于整数分歧。任何想法,一个优雅的解决这个问题的?
I have seen the article here: http://stackoverflow.com/questions/577427/evenly-divide-in-c, but it seems like it only works for integer divisions. Any idea of an elegant solution to this problem?
推荐答案
计算一次的金额之一,从总减去每金额确保你总是有正确的总左:
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
这篇关于用一个整数整除美元金额(十进制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!