Objective C数学

时间:2012-05-13 09:14:23

标签: objective-c ios math int double

到目前为止,我如何计算复利率?

 double principal = [[principalLabel text] doubleValue];
 NSLog(@"Principal: %lf",principal);
 double years = [[yearsLabel text] doubleValue];
 NSLog(@"Years: %lf",years);
 double months = [[monthsLabel text] doubleValue] / 12; 
 NSLog(@"Months: %lf",months);
 double days = ([[daysLabel text] doubleValue] / 365) / 10;
 NSLog(@"Days :%lf",days);
 double rate = ([[rateLabel text] doubleValue] / 100);
 NSLog(@"Rate: %lf",rate);
 double time = years + days + months;
 NSLog(@"Time: %lf",time);

 double total = pow(principal * (1 - rate), time);
 NSLog(@"Total: %lf",total);
 double interest = pow(principal * (1 - rate), time) - principal;
 NSLog(@"Interest: %lf",interest);
 NSString *interestString = [[NSString alloc] initWithFormat:@"%lf",interest];
 NSString *totalString = [[NSString alloc] initWithFormat:@"%lf",total];
 [interestLabel setText:interestString];
 [totalLabel setText:totalString];

所以你可以看到我有5个UITextFields:principal,rate,years,months,days。目前,我一直得到一些答案,即使我的数学看起来正确,但我已经完全回顾了我的代码并找不到任何解决方案。

My desired result is: E.g.     
M = P * (1+R)^Y              
M = 1000 * (1+0.10)^2       
M = 1210

1 个答案:

答案 0 :(得分:2)

如果您也将NSLog消息的输出放在您的问题中,那么回答您的问题会更有帮助。现在,下面提到了一个明显的错误:

在这行代码中

double total = pow(principal * (1 - rate), time);

你有1 - 率,而你需要

double total = pow(principal * (1 + rate), time);