我在 .net core 控制台应用程序中有这段代码:-
var currentDate = startdate;
while (currentDate <= enddate)
{
// Get the days in the current month
var daysInMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
// Calculate the number of days within the current month
var monthStart = currentDate;
var monthEnd = new DateTime(currentDate.Year, currentDate.Month, daysInMonth);
if (monthEnd > enddate) monthEnd = enddate;
var daysInCurrentMonth = Convert.ToDecimal(Math.Truncate((monthEnd - monthStart).TotalDays + 1));
// Proportion of the amount for this month
var monthlyAmount = decimal.Parse((daysInCurrentMonth / totalDays).ToString()) * Amount;
// Apply discount if applicable
monthlyAmount = Math.Round((Discount != null ? monthlyAmount * (1 - (Decimal.Parse(Discount.ToString()) / 100)) : monthlyAmount),3);
// Add to the result list
monthlyAmounts.Add(
(ContractType,
monthStart,
monthEnd,
monthStart.Month.ToString(),
monthStart.Year,
monthlyAmount,
NetworkID,
NetworkTitle,
MediumTitle,
(MediumID != null ? MediumID.ToString() : null),
BookingCalendarID,
BookingCalendarIDSalesRepIDMap
.SingleOrDefault(a => a.Key == BookingCalendarID).Value, daysInCurrentMonth, totalDays, monthlyAmount)
);
// Move to the next month
currentDate = monthEnd.AddDays(1);
}
金额货币为 JOD,最多保留 3 位小数。
我的问题是关于这两行代码:-
var monthlyAmount = decimal.Parse((daysInCurrentMonth / totalDays).ToString()) * Amount;
// Apply discount if applicable
monthlyAmount = Math.Round((Discount != null ? monthlyAmount * (1 - (Decimal.Parse(Discount.ToString()) / 100)) : monthlyAmount),3);
那么我的计算正确吗?例如,我有一份从
22/10/2024
开始到 4/11/2024
的合同,金额为 1000 JOD(有 3 位小数),折扣为 15%。所以当我想计算11月份的合同金额时,它将如下:-
每月金额 = 4/14 * 1,000 = 285.7142857142857
每月金额 = Math.Round(285.7142857142857 * 0.85,3) = 242.857 JOD
另外,当我计算 10 月份时,我会得到
607.142
.. 但是当我求和 242.857 + 607.142 = 849.999 .. 时,会损失 0.001 JOD .. 那么我该如何解决这个问题?
谢谢
我只是将代码放入 vs 中,并删除了所有转换并在双精度类型的基础上计算了所有内容。经过计算,我得到了所需的 850 JOD。
这是我使用的代码(必须添加一些变量来执行):
DateTime startdate = new DateTime(2024,10,22);
DateTime enddate = new DateTime(2024,11,4);
List<double> monthlyAmounts = new List<double>();
double Discount = 15.0d;
double Amount = 1000.0d;
var currentDate = startdate;
double totalDays = enddate.Subtract(startdate).TotalDays+1;
while (currentDate <= enddate)
{
// Get the days in the current month
var daysInMonth = DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
// Calculate the number of days within the current month
var monthStart = currentDate;
var monthEnd = new DateTime(currentDate.Year, currentDate.Month, daysInMonth);
if (monthEnd > enddate) monthEnd = enddate;
var daysInCurrentMonth = Math.Truncate((monthEnd - monthStart).TotalDays + 1);
// Proportion of the amount for this month
var monthlyAmount = (daysInCurrentMonth / totalDays) * Amount;
// Apply discount if applicable
monthlyAmount = Math.Round((Discount != null ? monthlyAmount * (1 - (Discount / 100)) : monthlyAmount), 3);
// Add to the result list
monthlyAmounts.Add(monthlyAmount);
// Move to the next month
currentDate = monthEnd.AddDays(1);
}
我假设所有类型转换在计算过程中可能会损失一点精度。如果您知道必须使用十进制数字,请尝试将所有必需的整数也以十进制为基础以避免转换损失:)