我对ceil()函数有疑问。我在下面的代码中尝试将C中的两个小数位向上舍入为变量monthlyPayment,并且输出中的ceil函数似乎被忽略。在图像中输出与它应该是的相比。 img
#include "stdafx.h"
#include "stdio.h"
#include "math.h"
#define MONTHS 12
int main()
{
double principle;
double rate;
double numYears;
double numMonths;
double monthlyPayment;
double monthlyRate;
printf("Welcome to the morgage calculator.\nEnter your principal amount to get started. ");
scanf_s("%lf", &principle);
printf("Enter the yearly morgage interest rate ");
scanf_s("%lf", &rate);
printf("Enter the number of years ");
scanf_s("%lf", &numYears);
numMonths = numYears * MONTHS;
monthlyRate = rate / MONTHS / 100;
monthlyPayment = ((monthlyRate) / (1 - pow((1 + monthlyRate), (-numMonths)))* principle);
monthlyPayment = monthlyPayment * 100;
ceil(monthlyPayment);
monthlyPayment = monthlyPayment / 100;
double toPrince = 0.0;
double interest = 0.0;
printf("Your total monthly payment ammount:$ %.2lf\n", monthlyPayment);
printf("Month\tPrinciple\tInterest\t $ to Principle\n");
for (int index = 1; index <= numMonths;) {
interest = principle * monthlyRate;
toPrince = monthlyPayment - interest;
printf("%2d%16.2lf %16.2lf %16.2lf\n", index, principle, interest, toPrince);
principle = principle - toPrince;
index++;
}
}
答案 0 :(得分:1)
ceil()
不会更改变量。
应该是:
double result = ceil(var);
答案 1 :(得分:1)
您可以通过观察C函数无法修改其参数来发现问题,除非您将指针传递给您想要修改的内容。这就是在调用scanf
时添加&符号的原因。
ceil(monthlyPayment)
中没有&符号,monthlyPayment
不是指针;因此,不可能进行修改。
您可以修改monthlyPayment
的汇总代码,如下所示:
monthlyPayment = ceil(monthlyPayment * 100) / 100;